PS:form urllib import request不知道为啥报错,导致没测试成功
urllib:提供一系列用于操作URL的功能
1.Get
(1)urllib的request模块:方便地抓取URL内容,发送一个GET请求到指定的页面,然后返回HTTP的响应
form urllib import requestwith request.urlopen('url') as f: data = f.open() print('Student:', f.status, f.reason) for k, v in f.getheaders(): print('%s, %s' % (k, v)) print ('Data:', data.decode('utf-8'))
(2)模拟浏览器发送GET请求,就需要使用Request对象,通过Request对象添加HTTP头,这样就可以把请求伪装成浏览器
# 模拟iPhone 6去请求豆瓣首页from urllib import requestreq = request.Request('url')req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')with request.urlopen(req) as f: print('Status:', f.status, f.reason) for k, v in f.getheaders(): print ('%s, %s' % (k, v)) print ('Data:', f,read().decode('utf-8'))
2.Post
以一个Post发送一个请求,只需要把参数data以bytes形式传入
# 模拟一个微博登录from urllib import request, parseprint ('Login to weibo.cn...')email = input('Email: ')passwd = input('Password: ')login_data = parse.urlencode([ ('username', email), ('password', passwd), ('entry', 'mweibo'), ('client_id', ''), ('savestate', '1'), ('ec', ''), ('pagerfer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')])req = request.Request('https://passport.weibo.cn/sso/login')req.add_headers('Origin', ''https://passport.weibo.cn')req.add_headers('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')req.add_headers('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')with request.urlopen(req, data=login_data.encode('utf-8')) as f: print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s, %s' % (k, v)) print ('Data: ', f.read().decode('utf-8'))