我怎样才能从python2 http请求代码更改为python3请求

时间:2012-07-29 14:42:49

标签: python python-3.x python-requests

我使用python2.6编写以下代码,我想将其更改为python3.2和Requests:

import urllib, urllib2
import cookielib 
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
urllib2.install_opener(opener)

params = urllib.urlencode(dict(x = '0' , y = '0', url='login.php', password='1234',id='userid'))   
f = urllib2.urlopen('http://test.com/login_check.php', params)  
data = f.read()   
login_response = opener.open('http://aaa.com/bbs/confirm.php?')
htmlContent = login_response.read()   
print htmlContent   
response.close()

1 个答案:

答案 0 :(得分:0)

如果您查看文档,那就非常简单:http://docs.python-requests.org/en/latest/index.html

import requests

url = r'http://test.com/login_check.php'
parameters = {'x' : '0' ,
              'y' : '0', 
              'url':'login.php',
              'password':'1234',
              'id':'userid'}

login_response = requests.get(url, params=parameters)
htmlContent = login_response.text