方法一:urllib2
参考:http://www.cnblogs.com/chenzehe/archive/2010/08/30/1812995.html
post:
#!/usr/bin/python
#coding=utf-8 import urllib
import urllib2 def post(url, data):
req = urllib2.Request(url)
data = urllib.urlencode(data)
#enable cookie
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
response = opener.open(req, data)
return response.read() def main():
posturl = "http://www.xiami.com/member/login"
data = {'email':'myemail', 'password':'mypass', 'autologin':'', 'submit':'登 录', 'type':''}
print post(posturl, data) if __name__ == '__main__':
main()
#该代码片段来自于: http://www.sharejs.com/codes/python/5756
get:
data['name'] = 'Somebody Here'
data['location'] = 'Northampton'
data['language'] = 'Python'
url_values = urllib.urlencode(data)
url = 'http://pythoneye.com/example.cgi'
full_url = url + '?' + url_values
data = urllib2.open(full_url)
2、response
resp = requests.get(url, {'level': level, 'starttime': starttime, 'endtime': endtime})
resp_dict = json.loads(resp.text)
resp_data = resp_dict['data']
参考:http://requests-docs-cn.readthedocs.org/zh_CN/latest/user/quickstart.html