python
1html = requests.get('https://npb.jp/bis/teams/rst_s.html') 2soup = BeautifulSoup(html.content, "html.parser") 3tds = soup.find_all(class_='rosterRegister') 4for result in tds: 5 link = result.find('a') 6 alink = link.get('href') 7 8 print(alink)
linkをprint(link)として出力すると
<a href="/bis/players/31735151.html">奥川 恭伸</a>
~
~
となっており、意図通りの結果が返ってきますが、そこからhrefだけを抽出しようとすると、none、空、というエラーになります。
AttributeError: 'NoneType' object has no attribute 'get'
似た処理としてyahooのリンクを取得するだけなら意図通りの動きになります。
python
1html = requests.get('https://yahoo.co.jp') 2soup = BeautifulSoup(html.content, "html.parser") 3result = soup.find('a') 4alink = result.get('href') 5print(alink) 6 7#https://yahoo.co.jp が返ってくる
違いが見いだせません。
問題点はどこにあるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/21 01:10