2009-10-12 26 views
21

我需要在目标页面上填写表单值,然后通过Python单击按钮。我看了Selenium和Windmill,但是这些都是测试框架 - 我没有测试。我试图以编程方式登录第三方网站,然后下载并解析我们需要插入到数据库中的文件。测试框架的问题在于它们启动了浏览器的实例;我只想要一个我可以安排每天运行的脚本来检索我想要的页面。任何方式来做到这一点?通过Python脚本在网页中填写表单值(不测试)

回答

21

您正在寻找Mechanize

形式提交样本:

import re 
from mechanize import Browser 

br = Browser() 
br.open("http://www.example.com/") 
br.select_form(name="order") 
# Browser passes through unknown attributes (including methods) 
# to the selected HTMLForm (from ClientForm). 
br["cheeses"] = ["mozzarella", "caerphilly"] # (the method here is __setitem__) 
response = br.submit() # submit current form 
+0

我坚持使用Python 2.6的,所以遗憾的是机械化是不是一种选择,无论是。 (GopherError在2.6下降,看起来像)。 – Habaabiai 2009-10-12 15:35:09

+0

机械化doc通常有点简洁,但它确实非常棒! – 2009-10-12 15:35:11

+0

我认为你应该坚持,试着调试gopher问题。在python 2.6中,gopher支持被移除了IIRC,所以解决你的问题可能是关于评论一些导入的gopherlib和实际使用gopher的少数几个点。 – 2009-10-12 15:38:17

3

您可以使用标准urllib库要做到这一点,像这样:

import urllib 

urllib.urlretrieve("http://www.google.com/", "somefile.html", lambda x,y,z:0, urllib.urlencode({"username": "xxx", "password": "pass"})) 
1

的机械化例子的建议似乎上班。在输入字段,你必须输入文字,使用类似:

print response.read() 
12

有:如果您提交的表格后,如在搜索引擎产生的一些内容

br["kw"] = "rowling" # (the method here is __setitem__) 

,您通过得到它看看这个例子使用机械化:它会给基本思想:

#!/usr/bin/python 
import re 
from mechanize import Browser 
br = Browser() 

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

# Retrieve the Google home page, saving the response 
br.open("http://google.com") 

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

# Get the search results 
br.submit() 

# Find the link to foofighters.com; why did we run a search? 
resp = None 
for link in br.links(): 
    siteMatch = re.compile('www.foofighters.com').search(link.url) 
    if siteMatch: 
     resp = br.follow_link(link) 
     break 

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