beautifulsoup return 空值

beautifulsoup return empty value

我正在使用 Jupyter Python 2.7 我试图从该网站检索数据,使用 beautifulsoup 和 lxml 解析器抓取描述或价格一切顺利。

站点 = 'https://www.bedbathandbeyond.com/store/product/dyson-v7-motorhead-cord-free-stick-vacuum-in-fuchsia-steel/1061083288?brandId=162'

然而,当我试图抓取评论者的评论或位置时,我无法检索到任何内容,只有一个空列表 []

我也试过先用PyQt4渲染,还是不行。我现在应该怎么解决?

我的代码附在下面

import PyQt4
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import * 
import sys
from lxml import html
from bs4 import BeautifulSoup
import os
import requests

site = 'https://www.bedbathandbeyond.com/store/product/dyson-v7-motorhead-cord-free-stick-vacuum-in-fuchsia-steel/1061083288?brandId=162'

class Render(QWebPage):     
    def __init__(self, url):
        self.app = QApplication(sys.argv)  
        QWebPage.__init__(self)  
        self.loadFinished.connect(self._loadFinished)  
        self.mainFrame().load(QUrl(url))  
        self.app.exec_()   
    def _loadFinished(self, result):  
        self.frame = self.mainFrame()  
        self.app.quit()
r = Render(site)  
result = r.frame.toHtml()
formatted_result = str(result.toAscii())
tree = html.fromstring(formatted_result)
soup = BeautifulSoup(formatted_result,'lxml')
soup.find_all('span', class_ = 'BVRRValue BVRRUserLocation')#return value is []

非常感谢!

我快速检查了引用的 URL,评论仅在您单击 "Ratings & Review" 选项卡后通过异步调用加载。因此,如果您只是在没有任何额外导航的情况下加载页面,评论将不会出现在 DOM 中(因此不会出现在您使用 BeautifulSoup 解析的 HTML 中)。

所以一个解决方案是在获取 HTML 并将其传递给 BeautifulSoup 之前触发对 "Ratings & Review" 的点击。

或者,您也可以进行相同的异步调用来自己获取评论。通过对该页面执行 GET 请求来检索评论的第一页:https://bedbathandbeyond.ugc.bazaarvoice.com/2009-en_us/1061083288/reviews.djs?format=embeddedhtml&page=1&scrollToTop=true.

您可以轻松地为 bedbathandbeyond 上的每个产品自行构造此 URL,因为您只需要具有产品 ID(在本例中为 1061083288),可以使用以下方法从原始 DOM 中轻松获取例如,id 为 prodRatings 的 div。它包含一个带有产品 ID 的隐藏输入字段。然后,您可以简单地将之前的 URL 替换掉,这样您就可以从所有产品中获取所有评论。