使用python提交到Web表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17509607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submitting to a web form using python
提问by Serial
I have seen questions like this asked many many times but none are helpful
我见过很多次这样的问题,但没有一个是有帮助的
Im trying to submit data to a form on the web ive tried requests, and urllib and none have worked
我试图将数据提交到网络上的表单我尝试过请求,而 urllib 和没有工作
for example here is code that should search for the [python] tag on SO:
例如,这里是应该在 SO 上搜索 [python] 标签的代码:
import urllib
import urllib2
url = 'http://stackoverflow.com/'
# Prepare the data
values = {'q' : '[python]'}
data = urllib.urlencode(values)
# Send HTTP POST request
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
html = response.read()
# Print the result
print html
yet when i run it i get the html soure of the home page
然而,当我运行它时,我得到了主页的 html 源代码
here is an example of using requests:
这是使用请求的示例:
import requests
data= {
'q': '[python]'
}
r = requests.get('http://stackoverflow.com', data=data)
print r.text
same result! i dont understand why these methods arent working i've tried them on various sites with no success so if anyone has successfully done this please show me how!
同样的结果!我不明白为什么这些方法不起作用我已经在各种网站上尝试过,但没有成功,所以如果有人成功做到了这一点,请告诉我怎么做!
Thanks so much!
非常感谢!
采纳答案by Johnsyweb
If you want to pass q
as a parameter in the URL using requests
, use the params
argument, not data
(see Passing Parameters In URLs):
如果你想通过q
在使用URL参数requests
,使用params
参数,而不是data
(见传递参数在网址):
r = requests.get('http://stackoverflow.com', params=data)
This will request https://stackoverflow.com/?q=%5Bpython%5D, which isn't what you are looking for.
这将请求https://stackoverflow.com/?q=%5Bpython%5D,这不是您要查找的内容。
You really want to POST
to a form. Try this:
你真的要POST
一个表格。尝试这个:
r = requests.post('https://stackoverflow.com/search', data=data)
This is essentially the same as GET
-ting https://stackoverflow.com/questions/tagged/python, but I think you'll get the idea from this.
这本质上与GET
-ting https://stackoverflow.com/questions/tagged/python相同,但我认为您会从中得到想法。
回答by Rashid Feroz
import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
This makes a POST request with the data specified in the values. we need urllib to encode the url and then urllib2 to send a request.
这将使用值中指定的数据发出 POST 请求。我们需要 urllib 来编码 url,然后 urllib2 来发送请求。
回答by Harsh Gupta
Mechanize library from python is also great allowing you to even submit forms. You can use the following code to create a browser object and create requests.
来自 python 的 Mechanize 库也很棒,允许您甚至提交表单。您可以使用以下代码创建浏览器对象并创建请求。
import mechanize,re
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.set_handle_refresh(False) # can sometimes hang without this
br.addheaders = [('User-agent', 'Firefox')]
br.open( "http://google.com" )
br.select_form( 'f' )
br.form[ 'q' ] = 'foo'
br.submit()
resp = None
for link in br.links():
siteMatch = re.compile( 'www.foofighters.com' ).search( link.url )
if siteMatch:
resp = br.follow_link( link )
break
content = resp.get_data()
print content