搜索Pastbin.com网站有了Mechaniz

2024-04-26 00:18:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我迄今为止所借用的代码,在上面很有用谷歌但不是开的pastebin.com网站想知道为什么我不能搜索pastebin.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

Tags: thebrimportbrowserreformcomfor
2条回答

您描述的问题可以通过提供有效的表单名称来解决:

br.select_form(name='search_form')

而且,你以后会有问题,虽然你试图抓住结果-但这是另一个问题的一部分。在

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

首页上没有所谓的“首页”。把源代码读到页面上,找出正确的名字。在

相关问题 更多 >