前提・実現したいこと
現在pythonでスクレイピングの勉強をしているのですが、一般的なサイトとgoogleページで同様の処理をやっているのですが、googleだけエラーが発生します。
なにか抜け道のような方法もあると思いますが、このコードでは取得できないような処理がされているのでしょうか?
発生している問題・エラーメッセージ
/url?q=http://career-theory.net/dont-want-to-go-to-work-5628&sa=U&ved=2ahUKEwiV1beCnfriAhWB51QKHXrqDeoQFjAAegQIBRAB&usg=AOvVaw1UOSDgboTy6yCM-RLRY8ZG --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-95-c91cb8ae4bf0> in <module>() 1 for tag in tags: ----> 2 print(tag.a.get("href")) AttributeError: 'NoneType' object has no attribute 'get'
該当のソースコード
python
1from bs4 import BeautifulSoup 2import requests 3import pandas as pd 4from google.colab import files 5 6columns = ['rank','title','url', 'affiliate_url'] 7df = pd.DataFrame(columns=columns) 8df.head() #配列の最初を取得,tail()は末尾 9 10keyword = '会社 行きたくない' 11html_doc = requests.get('https://www.google.co.jp/search?num=10&q=' +keyword).text 12soup = BeautifulSoup(html_doc, 'html.parser') # BeautifulSoupの初期化 13print(soup.prettify()) 14 15tags = soup.find_all("div",{"class":"jfp3ef"}) 16print (tags) 17 18↑ここまでは取得できる 19 20for tag in tags: 21 print(tag.a.get("href")) 22 23↑ここでエラー 24
import pandas as pd # pandasのインポート from bs4 import BeautifulSoup # BeautifulSoupのインポート import requests # requestsのインポート from google.colab import files url = "https://review-of-my-life.blogspot.com" # TODO1 requestsで、指定されたURLのHTMLを取得してください。 response = requests.get(url).text print(response) soup = BeautifulSoup(response,'html.parser') print(soup.prettify()) tags = soup.find_all("h3",{"class":"post-title"}) print(tags) for tag in tags: print(tag.a.get("href"))
試したこと
対象クラスを代えたり、aタグだけを直接指定したりしましたが、
getメソッドがきかず、リンクが容易に取得できないようになっているのかな?という印象です
補足情報(FW/ツールのバージョンなど)
Colab Notebooksで作成しています。
また、参考にしている記事は、下記の記事で、この記事の時点では検索結果に、h3などのタグがあったようですが、今はない?のか、上記コードでhtmlを開いてもでてこず、ただ、googleページで直接開発ツールを開いた場合は出てくるので、スクレイピング対策なのかな?と思いました。
https://note.mu/daikawai/n/n7cb363531396
参考記事では当時は、下記コードで
html_doc = requests.get('https://www.google.co.jp/search?num=10&q=' +keyword).text soup = BeautifulSoup(html_doc, 'html.parser') # BeautifulSoupの初期化 print(soup.prettify())
下記のように取得できたようです。今は、h3が出てきませんでした。
<ol> <div class="g"> <h3 class="r"> <a href="/url?q=http://career-theory.net/dont-want-to-go-to-work-5628&sa=U&ved=0ahUKEwiNk9LHldfdAhUowFkKHe1JDc4QFggUMAA&usg=AOvVaw2bQgJNPO5k35th4cb-eW8A"> <b> 会社 </b> に <b> 行きたくない </b> 5つの原因を対処・解決するための全知識 </a> </h3>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/24 22:47