Python的urllib2的自动填表和结果的检索 [英] Python urllib2 automatic form filling and retrieval of results

查看:184
本文介绍了Python的urllib2的自动填表和结果的检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待能够查询保修信息的网站,一台机器,该脚本将上运行的。如果需要的话(像在说,惠普的服务站点的情况下),它应该能够填写一个表格,然后将能够检索结果网页。

I'm looking to be able to query a site for warranty information on a machine that this script would be running on. It should be able to fill out a form if needed ( like in the case of say HP's service site) and would then be able to retrieve the resulting web page.

我已经有地方能够解析报告回我只是具有需要为了做到这一点需要被放置在字段,然后是数据的POST做什么麻烦生成的HTML中的位能够检索结果页面。

I already have the bits in place to parse the resulting html that is reported back I'm just having trouble with what needs to be done in order to do a POST of data that needs to be put in the fields and then being able to retrieve the resulting page.

推荐答案

如果你完全需要可使用的urllib2,基本要点是:

If you absolutely need to use urllib2, the basic gist is this:

import urllib
import urllib2
url = 'http://whatever.foo/form.html'
form_data = {'field1': 'value1', 'field2': 'value2'}
params = urllib.urlencode(form_data)
response = urllib2.urlopen(url, params)
data = response.read()

如果你沿着POST发送数据(第二个参数的urlopen()),请求方法将自动设置为POST。

If you send along POST data (the 2nd argument to urlopen()), the request method is automatically set to POST.

我建议你做你帮个忙,并使用机械化,一个完全成熟的urllib2替代其作用就像一个真正的浏览器。很多站点使用隐藏域,cookie和重定向,其中没有的urllib2的默认情况下,其中机械化为你做处理。

I suggest you do yourself a favor and use mechanize, a full-blown urllib2 replacement that acts exactly like a real browser. A lot of sites use hidden fields, cookies, and redirects, none of which urllib2 handles for you by default, where mechanize does.

查看在Python模拟一个浏览器机械化以一个很好的例子。

Check out Emulating a browser in Python with mechanize for a good example.

这篇关于Python的urllib2的自动填表和结果的检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆