使用beautifulsoup刮除html错误“div”未定义

时间:2017-02-01 01:35:07

标签: python beautifulsoup

我正在尝试在div类下提取信息,但是当我使用代码时,消息显示“div”未定义。汤工作正常,我看到旁边有很多div,可能是什么问题?

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

1 个答案:

答案 0 :(得分: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'])