我试图刮掉所有存在于网页中的项目的名称,但在默认情况下只有18是在网页上看到和我的代码只有那些刮。 您可以通过点击“全部显示”按钮来查看所有项目,但该按钮是在Javascript。
经过一番研究,我发现PyQt的模块可以用来解决这个问题涉及JavaScript的按钮和我用它,但我仍然不能够调用“上点击”事件。 下面是提到的代码:
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" # This is the css selector you actually need
allButton = r.frame.documentElement().findFirst(allSelector)
allButton.evaluateJavaScript(jsClick)
page = allButton
soup = BeautifulSoup(page)
soup.prettify()
with open('Smartphones_26decv1.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])
我现在面临的这个错误如下:
"Invalid Syntax" Error for evObj
是否有人可以帮助我在调用此“的onclick”事件,以便我能为我的新节目凑所有items.Pardon我的数据我的无知。