【发布时间】:2019-02-26 00:59:27
【问题描述】:
我正在尝试从该 URL https://99airdrops.com/page/1/ 中提取数据。
我写的代码如下。
import requests
from bs4 import BeautifulSoup
url_str = 'https://99airdrops.com/page/1/'
page = requests.get(url_str, headers={'User-Agent': 'Mozilla Firefox'})
# soup = BeautifulSoup(page.text, 'lxml')
soup = BeautifulSoup(page.text, 'html.parser')
# print(soup.prettify())
print(len(soup.findAll('div')))
print(soup.find('div', class_='title'))
我的问题是print(len(soup.findAll('div'))) 行仅返回 23,而print(soup.find('div', class_='title')) 行打印None。即使有多个实例,find 命令也找不到带有class_='title' 的 div 元素,并且 div 元素深深嵌套在 html 页面中,但这从未给我带来过问题。
我尝试过使用lxml 和html.parser,但都没有返回所有的div 元素。我还尝试将 html 写入文件,将其读入,然后使用它运行 BeautifulSoup,但我得到了相同的结果。谁能告诉我这是什么问题?
我也尝试了这里的建议Beautiful Soup - `findAll` not capturing all tags in SVG (`ElementTree` does) 来更新我的 lxml 包,但我仍然遇到了同样的问题。
我也尝试了BeautifulSoup doesn't find correctly parsed elements这里的解决方案,但没有运气。
【问题讨论】:
-
您具体要获取哪些数据?
-
每个条目的标题、价格和日期。
标签: python html beautifulsoup