2

Spynner を使用して Web ページをスクレイピングし始めたばかりで、適切なチュートリアルが見つかりません。Google に単語を入力し、結果のページを表示する簡単な例をここに示します。

しかし、ボタンをクリックしてから実際に新しいページを取得するにはどうすればよいでしょうか?

import spynner

def content_ready(browser):
    if 'gbqfba' in browser.html:
        return True #id of search button

b = spynner.Browser()
b.show()
b.load("http://www.google.com", wait_callback=content_ready)
b.wk_fill('input[name=q]', 'soup')
# b.browse() # Shows the word soup in the input box
with open("test.html", "w") as hf: # writes the initial page to a file
    hf.write(b.html.encode("utf-8"))
b.wk_click("#gbqfba") # Clicks the google search button (or so I think)

しかし、今は何ですか?id=gbqfba はありますが、Google 検索ボタンをクリックしたかどうかさえわかりません。私も b.click("#gbqfba") だけを試しました。検索結果を取得するにはどうすればよいですか?

私はちょうどやってみました:

 with open("test.html", "w") as hf: # writes the initial page to a file
    hf.write(b.html.encode("utf-8"))

ただし、それでも最初のページが印刷されます。

4

2 に答える 2

2

Enter を入力に送信し、2 秒間待機することでこれを解決しました。理想的ではありませんが、機能します

import spynner
import codecs
from PyQt4.QtCore import Qt

b = spynner.Browser()
b.show()
b.load("http://www.google.com")
b.wk_fill('input[name=q]', 'soup')
# b.browse() # Shows the word soup in the input box

b.sendKeys("input[name=q]",[Qt.Key_Enter])
b.wait(2)
codecs.open("out.html","w","utf-8").write(b.html)
于 2013-08-11T21:30:29.313 に答える
1

推奨される方法は、新しいページが読み込まれるまで待つことです。

b.wait_load()
于 2015-04-12T00:55:12.743 に答える