urllib模块只能用get
urllib2模块既有get也有post
网址url = urllib2.urlopen(‘
http://www.zhanjibao.com/core/search/player',data = 'nickname=%E9%9B%AA%E5%9F%9F%E6%9E%AB
‘)有data表示post请求
urllib2模块
request请求是反爬虫
add—header中要写成字典的形式,用逗号隔开,单引号引起两部分的内容
req = urllib2.Request('http://www.zhanjibao.com/core/search/player',data = 'nickname=%E9%9B%AA%E5%9F%9F%E6%9E%AB')
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0')
data
1
2
3
4
5
6
7
8
9
|
import urllib
import urllib2
values = {"username":"1016903103@qq.com","password":"XXXX"}
data = urllib.urlencode(values)
url = "https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"
request = urllib2.Request(url,data)
response = urllib2.urlopen(request)
print response.read()
|
get
4
5
6
7
8
9
10
11
12
|
import urllib
import urllib2
values={}
values['username'] = "1016903103@qq.com"
values['password']="XXXX"
data = urllib.urlencode(values)
url = "http://passport.csdn.net/account/login"
geturl = url + "?"+data
request = urllib2.Request(geturl)
response = urllib2.urlopen(request)
print response.read()
|