python - 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 download
html 类中。
解决方案
如果你想要一个链接,它将 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 是什么。
推荐阅读
- android - 如何只允许特定的电子邮件帐户登录您的 Android 应用程序?
- ruby-on-rails - 如何在 ruby on rails 中将浮点数渲染为分数
- qt - QPainter - 绘制带有多个孔的多边形
- reactjs - ReactJS 事件有时有效,有时无效
- elasticsearch - 从 ElasticSearch 中该索引中的两个现有字段在所有文档中创建新字段
- r - 缺少列的 R dplyr 子集
- python - 迭代器和枚举器对象之间的区别
- r - 如何在 R 中绘制多个时间序列图
- typescript - 抽象类实现的 Typescript 类型声明
- android - 在颤振中从数据库生成表单