我需要在目标页面上填写表单值,然后通过Python单击按钮。我看了Selenium和Windmill,但是这些都是测试框架 - 我没有测试。我试图以编程方式登录第三方网站,然后下载并解析我们需要插入到数据库中的文件。测试框架的问题在于它们启动了浏览器的实例;我只想要一个我可以安排每天运行的脚本来检索我想要的页面。任何方式来做到这一点?通过Python脚本在网页中填写表单值(不测试)
21
A
回答
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
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
相关问题
- 1. 自动填写表单测试网页表单
- 2. Python脚本加载测试网页
- 3. 通过填写不同网站上的表单登录网站
- 4. 使用Python填写网页表格
- 5. 填写CSS表单测试的URL
- 6. 测试Python脚本
- 7. 网站负载测试Python脚本
- 8. 如何在使用JavaScript的网页上填写网页表单
- 9. 填写浏览器表单的脚本
- 10. 创建脚本填写JavaScript表单
- 11. 谷歌应用程序脚本填写网站表单字段
- 12. IE6自动网页表单测试/填充专家6
- 13. 在Django/Python中编写单元测试
- 14. 在Python 3中测试学生脚本
- 15. 在Python中测试数据库读/写加分析脚本
- 16. IOS没有显示在网页视图填充PDF表单填写文本
- 17. 通过Python脚本
- 18. Python脚本通过导入它来测试DLL的功能
- 19. python网页黑箱测试
- 20. Jquery填写表单输入文本值
- 21. 如何在Android中的网页上填写表单?
- 22. Python网页中的Python搜索脚本
- 23. Python urllib,urllib2填写表单
- 24. Python测试整个脚本
- 25. 通过谷歌脚本在google电子表格中填写多行
- 26. 在jsp中重新填写表单值
- 27. 通过网页的最终用户可配置的python脚本
- 28. 为什么python脚本无法通过代理下载网页
- 29. 编写单元测试通过测试IEntityWithChangeTracker(EF4)
- 30. php测试脚本通过jquery,将值传递给php文件
我坚持使用Python 2.6的,所以遗憾的是机械化是不是一种选择,无论是。 (GopherError在2.6下降,看起来像)。 – Habaabiai 2009-10-12 15:35:09
机械化doc通常有点简洁,但它确实非常棒! – 2009-10-12 15:35:11
我认为你应该坚持,试着调试gopher问题。在python 2.6中,gopher支持被移除了IIRC,所以解决你的问题可能是关于评论一些导入的gopherlib和实际使用gopher的少数几个点。 – 2009-10-12 15:38:17