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

2024-10-08 19:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在div类下提取信息,但是当我使用代码时,消息显示“div”未定义。汤做得很好,我看到旁边有很多小酒鬼,有什么问题吗?你知道吗

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

Tags: 代码indiv信息parser消息forhtml
1条回答
网友
1楼 · 发布于 2024-10-08 19:24:03

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'])

相关问题 更多 >

    热门问题