我正在尝试使用漂亮的汤从网站上抓取数据。默认情况下,此网页显示 18 个项目,单击 javascript 按钮“showAlldevices”后,所有 41 个项目都可见。Beautiful Soup 只为默认情况下可见的项目抓取数据,以获取我使用 PyQt 模块并使用 javascript 代码调用 click 事件的所有项目的数据。以下是参考代码:
import csv
import urllib2
import sys
import time
from bs4 import BeautifulSoup
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
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()
url = 'http://www.att.com/shop/wireless/devices/smartphones.html'
r = Render(url)
jsClick = """var evObj = document.createEvent('MouseEvents');
evObj.initEvent('click', true, true );
this.dispatchEvent(evObj);
"""
allSelector = "a#deviceShowAllLink"
allButton = r.frame.documentElement().findFirst(allSelector)
allButton.evaluateJavaScript(jsClick)
html = allButton.webFrame().toHtml()
page = html
soup = BeautifulSoup(page)
soup.prettify()
with open('Smartphones_26decv2.0.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow(["Date","Day of Week","Device Name","Price"])
items = soup.findAll('a', {"class": "clickStreamSingleItem"},text=True)
prices = soup.findAll('div', {"class": "listGrid-price"})
for item, price in zip(items, prices):
textcontent = u' '.join(price.stripped_strings)
if textcontent:
spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%A") ,unicode(item.string).encode('utf8').strip(),textcontent])
我正在使用这行代码将 html 喂给漂亮的汤html = allButton.webFrame().toHtml()
此代码运行时没有任何错误,但我仍然没有获得输出 csv 中所有 41 个项目的数据
我还尝试使用这些代码行将 html 喂给漂亮的汤:
allButton = r.frame.documentElement().findFirst(allSelector)
a = allButton.evaluateJavaScript(jsClick)
html = a.webFrame.toHtml()
page = html
soup = BeautifulSoup(page)
但是我遇到了这个错误:html = a.webFrame.toHtml()
AttributeError: 'QVariant' object has no attribute 'webFrame'
如果我在这里问任何基本问题,请原谅我的无知,因为我是编程新手并帮助我解决这个问题。