Python 2.7 在使用PyQt和beautiful soup进行网络垃圾处理时出现的问题
我正在使用PyQt和BeautifulSoup的组合从网页上刮取数据。PyQt被用作Python和Javascript之间的解释器。我正在调用一个“onclick”事件,并试图在“click”事件之后将该html提供给Beautiful soup。以下是参考代码:Python 2.7 在使用PyQt和beautiful soup进行网络垃圾处理时出现的问题,python-2.7,onclick,pyqt,screen-scraping,Python 2.7,Onclick,Pyqt,Screen Scraping,我正在使用PyQt和BeautifulSoup的组合从网页上刮取数据。PyQt被用作Python和Javascript之间的解释器。我正在调用一个“onclick”事件,并试图在“click”事件之后将该html提供给Beautiful soup。以下是参考代码: import csv import urllib2 import sys import time from bs4 import BeautifulSoup from PyQt4.QtGui import * from PyQt4
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.frame.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])
现在运行此命令后,我发现以下错误:
File "D:\Microsoft\Pricing\2012-12-26\AT&T_attempt2code.py", line 32, in <module>
html = allButton.frame.toHtml()
AttributeError: 'QWebElement' object has no attribute 'frame'
文件“D:\Microsoft\Pricing\2012-12-26\AT&T\u attempt2code.py”,第32行,在
html=allButton.frame.toHtml()
AttributeError:'QWebElement'对象没有属性'frame'
请帮助我解决此问题,并请原谅我的无知,因为我是编程新手。如错误消息中所述,问题在以下行中:
html = allButton.frame.toHtml()
allButton
没有frame
属性,因为它是QWebElement
的一个实例(其定义上的转换顺序是Render
->QWebFrame
->QWebElement
->QWebElement
)
在代码中,frame
属性在Render.\u loadFinished
方法中定义,因此只有r
对象具有frame
属性
如果将html
定义更改为:
html = r.frame.toHtml()
或:
html = allButton.webFrame().toHtml()
这只是pyqt的问题,与BeautifulSoup无关。@MartijnPieters感谢您的建议,标签也相应更改。你能帮我解决这个问题吗?我急需解决这个问题。对不起,我自己没有使用QWebElement的经验。你能不能把这个问题介绍给可能帮助我解决这个问题的人。这就是标签的用途。还有耐心。现在是圣诞节假期,所以可能需要更长的时间。我尝试使用这个
html=allButton.webFrame().toHtml()
。我并没有得到任何错误,但我得到的输出只是18个默认项目,默认情况下在这个网页上是可见的。我正在使用PyQt和evaluateJavascript调用onclick事件来显示网页上的所有设备,以便能够提取所有设备的数据。请通读上面的代码并帮助我解决这个问题。让我们把事情做好。如果这个答案似乎对你有用,那么请投票/接受它。为了获得有关新问题的帮助,您应该公布它,创建一个带有描述性标题的新问题,以便其他用户可以看到它并参与解决方案的搜索。谢谢您的建议,我已经提出了新问题@请仔细阅读