【发布时间】:2013-11-17 18:18:34
【问题描述】:
我用请求和 BeautifulSoup 编写了一个网络爬虫,但 DOM 中有一个我找不到的元素。
这是我的工作:
import requests
from bs4 import BeautifulSoup
r = requests.get('http://www.decitre.fr/rechercher/result/?q=victor+hugo&search-scope=3')
soup = BeautifulSoup(r.text)
我找不到的元素是“旧价格”(被删除的那个),当我使用浏览器开发工具检查 DOM 时可以看到它。
soup.find_all(class_='old-price') # returns [], no matter if I specify "span"
此外,我看不到汤中的“旧价格”字符串或请求的结果:
'old-price' in soup.text # False
'old-price' in r.text # False
当我用wget 获取源时我也看不到它。
我可以得到它的 div 父级,但在其中找不到价格子级:
commands = soup.find_all(class_='product_commande')
commands[0].find_all('old-price') # []
所以我不知道发生了什么。我错过了什么?
- 我用错了 request/BeautifulSoup 吗? (我不确定 r.text 是否返回完整的 html)
- 那个html部分是用javascript代码生成的吗?如果是这样,我怎么知道它,有没有办法获得完整的 html ?
非常感谢
【问题讨论】:
-
可能是在执行一些
javascript代码后生成了old-price元素。 -
这是一个动态加载的 JavaScript 元素,所以可以尝试使用 python Ghost[jeanphix.me/Ghost.py/] 加载网站,然后通过 BeautifulSoup(或通过 JS-Query 的 Ghost)解析它的内容
-
看起来 Ghost 是最好的选择,谢谢。 «在 webkit 框架内执行 javascripts 是 Ghost 提供的最有趣的功能之一»。会尽快尝试。
标签: python web-scraping beautifulsoup python-requests