urllib用法
简单方法
importurllib2
response = urllib2.urlopen("http://www.baidu.com")
printresponse.read()
构造request
importurllib2
request = urllib2.Request("http://www.baidu.com")
response = urllib2.urlopen(request)
printresponse.read()
get请求
importurllibimporturllib2
values = {}values["username"] ="anthoy@gmail.com"
values["passwd"] ="123456"
#编码values字典,格式化
data = urllib.urlencode(values)printdata
#构建get访问地址
geturl ="http://www.baidu.com"+"?"+data
printgeturl
request = urllib2.Request(geturl)respose = urllib2.urlopen(request)
post请求
importurllib
importurllib2
values = {}values['username'] ="anthony@gmail.com"
values['password'] ="123456"
#编码values字典,格式化
data = urllib.urlencode(values)printdataurl ="http://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"
#构建post请求
request = urllib2.Request(url, data)
printrequest
response = urllib2.urlopen(request)
printresponse.read()