使用 PyQT QWebElement 提交 Google

发布于 2024-09-18 18:48:24 字数 1275 浏览 3 评论 0原文

以下代码不会到达 searchResults。我已经打印出 documentElement.findFirst('input[name="btnG"]') 并发现它是 所以到目前为止我们都做得很好。请注意,我的目标不是抓取 Google,而是通过众所周知的公共 Google 来学习更简单。

#!/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_()

The following code does not reach searchResults. I have printed out documentElement.findFirst('input[name="btnG"]') and found it to be <input name="btnG" type="submit" value="Google Search" class="lsb"> so we are good up to that point. Note that my goal is not to scrape Google but it's simpler to learn via the well known and public Google.

#!/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_()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-09-25 18:48:24

我终于想通了!并提交到 http://drupal4hu.com/node/266

I have finally figured it out! and submitted to http://drupal4hu.com/node/266

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文