找不到带请求的元素/ BeautifulSoup

时间:2013-11-06 13:25:54

标签: python web-scraping beautifulsoup python-requests

我用一个请求和BeautifulSoup写了一个web scraper,而且我找不到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') # []

所以我不知道发生了什么。我错过了什么?

  • 我是否严重使用请求/ BeautifulSoup? (我不确定r.text是否返回完整的html)
  • 是用javascript代码生成的html部分吗?如果是这样我怎么知道它,有没有办法获得完整的HTML?
非常感谢

1 个答案:

答案 0 :(得分:0)

在我的情况下,我将无效的HTML传递给Beautiful Soup,导致它忽略文档开头的无效标记后的所有内容:

<!--?xml version="1.0" encoding="iso-8859-1"?-->

请注意,我也在使用Ghost.py。以下是我删除标记的方法。

#remove invalid xml tag
ghostContent = ghost.content
invalidCode = '<!--?xml version="1.0" encoding="iso-8859-1"?-->'
if ghostContent.startswith(invalidCode):
    ghostContent = ghostContent[len(invalidCode):]

doc = BeautifulSoup(ghostContent)     

#test to see if we can find text   
if 'Application Search Results' in doc.text:
    print 'YES!'