前提・実現したいこと
【前提】
python初心者です。
お手柔らかにお願いします。
ご不明な点等ありましたら迅速に回答しますのでご助力のほどよろしくお願いいたします。
【実現したいこと】
特定の文字を条件に抽出する処理をforでループさせたい
発生している問題・エラーメッセージ
IndexError Traceback (most recent call last) <ipython-input-115-9728897de535> in <module>() 10 print(type(tags[0])) 11 for tag in tags: ---> 12 print (tag.select("a")[0].get("href")) IndexError: list index out of range
該当のソースコード(全体)
from bs4 import BeautifulSoup import requests as rq keyword='プログラミング' html_orgin = rq.get('https://www.google.co.jp/search?num=10&q=' +keyword).text soup = BeautifulSoup(html_orgin, 'html.parser') tags = soup.find_all('div',{'class':'jfp3ef'}) print(len(tags)) #出力→36 print(type(tags[0])) #出力→<class 'bs4.element.Tag'> for tag in tags: print (tag.select("a")[0].get("href"))
上記コード実行結果
36 <class 'bs4.element.Tag'> /url?q=https://www.sejuku.net/blog/2100&sa=U&ved=2ahUKEwjM8OqxgaDjAhVHE6YKHce-Bw0QFjAAegQICRAB&usg=AOvVaw10tcKqr94eEhAiHqo1Wee9 /url?q=https://www.sejuku.net/blog/diagnose/%3Fcid%3Dsp_bar_red&sa=U&ved=2ahUKEwjM8OqxgaDjAhVHE6YKHce-Bw0Q0gIwAHoECAkQAw&usg=AOvVaw3k9bMAztgIpnftAlwx8E13 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-115-9728897de535> in <module>() 10 print(type(tags[0])) 11 for tag in tags: ---> 12 print (tag.select("a")[0].get("href")) IndexError: list index out of range
途中まではループ処理されていると思うのですが・・
ご助力お願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/06 10:21