我刚刚开始使用 Spynner 来抓取网页,但没有找到任何好的教程。我在这里有一个简单的例子,我在谷歌中输入一个词,然后我想查看结果页面。
但是我如何从单击按钮到实际获取新页面?
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。我也试过 b.click("#gbqfba")。如何获得搜索结果?
我试过做:
with open("test.html", "w") as hf: # writes the initial page to a file
hf.write(b.html.encode("utf-8"))
但这仍然会打印初始页面。