2017-02-01 7 views
0

divクラスの下で情報を抽出しようとしていますが、コードを使用するとメッセージが表示されません。スープはうまく動作し、側にたくさんのdivがあることがわかりますが、何が問題なのでしょうか?beautifulsoupを使用してhtmlエラー "div"を定義していません。

soup = BeautifulSoup(html, "html.parser") 
for item in soup.find_all("div", attrs={"class" : "article-content"}): 
     print(div.find("a")['href']) 

答えて

2

div変数が実際に定義されていませんでした、あなたの代わりにitemを使用するためのもの:

for item in soup.find_all("div", attrs={"class" : "article-content"}): 
    print(item.find("a")['href']) # or item.a['href'] 

それとも、あなたはCSS selectorと直接のリンクを取得することができます。

for a in soup.select("div.article-content a"): 
    print(a['href']) 
+0

ありがとう!私は実際にそれを数分間見た後にそれを考え出しました、はい、私はそれがdiv – song0089

関連する問題