首页 > 解决方案 > BeautifulSoup 类发现返回无

问题描述

我正在使用 BeautifulSoup 编写一个 python 程序,它将检索网站上的下载链接。我正在使用 find 方法来检索链接所在的 html 类,但它返回 None。

我曾尝试使用父类访问此类,但没有成功。

这是我的代码

link = 'https://data.worldbank.org/topic/agriculture-and-rural-development?view=chart'

for link in indicator_links:
    indicator_page = requests.get(link)
    indicator_soup = BeautifulSoup(page.text, 'html.parser')
    download = indicator_soup.find(class_="btn-item download")

同样,我希望下载链接位于btn-item downloadhtml 类中。

标签: pythonweb-scrapingbeautifulsoup

解决方案


如果你想要一个链接,它将 100% 在 < a > 标签中。这是我能做的最好的帮助:

from bs4 import BeautifulSoup
import urllib.request

page_url = "https://data.worldbank.org/topic/agriculture-and-rural-development?view=chart"
soup = BeautifulSoup(urllib.request.urlopen(page_url), 'lxml')

what_you_want = soup.find('a', clas_="btn-item download")

这应该会给你你想要的链接。

不确定您要在代码中做什么,因为我不知道 indicator_links 是什么。


推荐阅读