1、构造request并请求页面
import urllib2
url =
"http://www.126.com/"
request = urllib2.Request(url)
response = urllib2.urlopen(request)
print response.read()
2、POST和GET数据传送
2.1 post方式:
import urllib
import urllib2
"password":
"XXXXXX"}
data = urllib.urlencode(values) #将字典values进行编码,这样才可以进行post传输
url =
"https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"
request = urllib2.Request(url, data)#将data作为Request的第二个参数
response = urllib2.urlopen(request)
print response.read()
2.2 get方式
import urllib
import urllib2
"password":
"XXXXXX"}
data = urllib.urlencode(values)
url =
"https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"
geturl = url +
"?" + data #这里就只是把编码后的values写入原来的url后,中间加一个 ? 号
print geturl
request = urllib2.Request(geturl)
response = urllib2.urlopen(request)
print response.read()