1.urllib2简介
urllib2提供一个基础函数urlopen,通过向指定的URL发出请求来获取数据。最简单的形式就是
import urllib2
response = urllib2.urlopen(url)
html = response.read()
基于请求响应模型,上述过程可以分为两步:
1.指定一个域名,并发送一个请求。
request=urllib2.request(url)
2.服务器端响应来自客户端的请求。
response = urllib2.urlopen(request)
2.向网页提交数据的请求。urllib2翻译为
import urllib
import urllib2
url = 'http://www.douban.com'
info = {'name' :' Michel','location':'Beijing'}
data = urllib.urlencode(info)
req= urllib2.Request(url,data)
response = urllib2.urlopen(req)
the_page = response.read()
3,伪装信息包含在请求头header中
import uellib
import urllib2
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; W indows NT)'
values = {'name' : 'Michael Foord', 'location' : 'Northampton','language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
模拟登录人人网
