سؤال

الكود التالي لا يصل إلى SearchResults. لقد قمت بطباعة documentelement.findfirst ('input [name = "btng"]') ووجدته ليكون <input name="btnG" type="submit" value="Google Search" class="lsb"> لذلك نحن جيدون حتى هذه النقطة. لاحظ أن هدفي ليس كشط 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_()
هل كانت مفيدة؟

المحلول

لقد اكتشفت أخيرًا! وتقديم إلى http://drupal4hu.com/node/266

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top