HTTP:超文本传输协议,设计的目的是保证客户端于服务端之间的通信。
HTTP的工作方式就是客户端与服务器之间的请求-应答协议。
两种最常见的请求方法就是GET、POST请求。
以下是对GET、POST请求的模拟:
GET:
运用urllib2
import urllib2
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
运用requests
import requests
r = requests.get(url, params=params)
POST:
运用urllib, urllib2
import urllib, urllib2
url = "www.xxx.xxx"
values = {.....,
.....,
....}
headers = {'xxxxxx':xxxxxx}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
reponse = urllib2.urlopen(req)
the_page = reponse.read()
运用requests
import requests
data = {...............}
r = requests.post(url, data=data, headers=headers)