使用PyQT QWebElement提交Google

时间:2010-09-12 17:26:54

标签: python pyqt qwebview qwebelement

以下代码未达到searchResults。我打印出documentElement.findFirst('input [name =“btnG”]')并发现它是<input name="btnG" type="submit" value="Google Search" class="lsb">所以我们很高兴到那一点。请注意,我的目标不是要刮掉谷歌,而是通过众所周知的公共谷歌来学习它。

#!/usr/bin/python
from PyQt4.QtCore import QUrl, SIGNAL
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage, QWebView

class Scrape(QApplication):
  def __init__(self):
    super(Scrape, self).__init__(None)
    self.webView = QWebView()
    self.webView.loadFinished.connect(self.searchForm)

  def load(self, url):
    self.webView.load(QUrl(url))

  def searchForm(self):
    documentElement = self.webView.page().currentFrame().documentElement()
    inputSearch = documentElement.findFirst('input[title="Google Search"]')
    inputSearch.setAttribute('value', 'test')
    self.webView.loadFinished.disconnect(self.searchForm)
    self.webView.loadFinished.connect(self.searchResults)
    documentElement.findFirst('input[name="btnG"]').evaluateJavaScript('click()')

  def searchResults(self):
    for element in documentElement.find('li[class="g"]'):
      print unicode(element.toOuterXml())
    self.exit()

my_scrape = Scrape()
my_scrape.load('http://google.com/ncr')
my_scrape.exec_()

1 个答案:

答案 0 :(得分:0)

我终于明白了!并提交给http://drupal4hu.com/node/266