【发布时间】:2017-11-17 18:55:18
【问题描述】:
这个问题类似于this one。 我已经阅读了答案,但没有一个对我有用。 我正在尝试从this site 的蓝色框中获取信息。
这是我写的:
import requests
from bs4 import BeautifulSoup
import re
url = 'https://boardgamegeek.com/boardgame/161936/pandemic-legacy-season-1'
req = requests.get(url)
soup = BeautifulSoup(req.text,'html5lib')
soup = soup.find('div', class_='game-header-body')
print(soup.prettify())
我收到此错误AttributeError: 'NoneType' object has no attribute 'prettify'。原因是因为找不到'game-header-body',所以变成NoneType。当我删除soup = soup.find('div', class_='game-header-body') 行时,我可以看到除我感兴趣的 div 之外的所有 html 代码。
我已经读过,也许改用“html5lib”解析器库会更好。我通过pip3 install html5lib 安装了它(我使用的是python 3.4.3),但仍然出现上述错误。我该怎么办?
【问题讨论】:
-
您好,页面源中不存在 game-header-body 元素,它是由 javascript 加载的。所以,你需要 selenium,它会加载 javascript,然后你可以提取。
标签: python html python-3.x parsing beautifulsoup