python网页提交表单,使用python提交到Web表单

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

for example here is code that should search for the [python] tag on SO:

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

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!

解决方案

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):

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.

You really want to POST to a form. Try this:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值