2014-09-02 70 views
0

这是迄今为止我已经借了代码的伟大工程在Google.com上而不是在pastebin.com会喜欢上我为什么不能搜索pastebin.com一些输入搜索Pastbin.com与机械化

import re 
from mechanize import Browser 
br = Browser() 

# Ignore robots.txt 
br.set_handle_robots(False) 
# user-agent that isn't a robot 
br.addheaders = [('User-agent', 'Firefox')] 

# Retrieve the web page 
br.open("http://pastebin.com") 

# Select the search box and search for 'foo' 
br.select_form('f') 
br.form[ 'q' ] = 'facebook' 

# Get the search results 
br.submit() 

# Find the link 
resp = None 
for link in br.links(): 
    siteMatch = re.compile('www.facebook.com').search(link.url) 
    if siteMatch: 
     resp = br.follow_link(link) 
     break 

# Print the site 
content = resp.get_data() 
print content 
+0

这是什么意思,你不能搜索?任何错误? – alecxe 2014-09-02 19:41:41

+0

select_form中的文件“build/bdist.macosx-10.3-fat/egg/mechanize/_mechanize.py”,第524行,在select_form中 mechanize._mechanize.FormNotFoundError:没有表单匹配名称'f' – 2014-09-02 19:42:37

+0

完成任何基本的调试, pastebine发送你的脚本?你只是假设什么都不会出错。 – 2014-09-02 19:42:41

回答

0
br.select_form('f') 
br.form[ 'q' ] = 'facebook' 

Pastebin主页上没有称为“f”的表单。阅读页面的来源以找到正确的名称。

+0

'q'实际上是搜索字段框的名称 - OP在这里很幸运。 – alecxe 2014-09-02 19:50:22

+0

糟糕!混淆了课程和名称。所以这一部分是正确的,但只是偶然。 – duskwuff 2014-09-02 19:53:44

+0

感谢您的帮助和建议br.select_form('f') br.form ['q'] ='facebook'应该是 – 2014-09-02 20:26:40

0

你所描述的可以通过提供一个有效的形式,名称要解决的问题:

br.select_form(name='search_form') 

而且,你以后会出现问题,而试图抓住的结果 - 但是这是一个不同的问题的一部分。

+0

这似乎工作,但现在的问题抓住结果谢谢你的帮助,我将继续堵塞现在。 – 2014-09-02 20:26:08