python urllib2/urllib实现

本文介绍了Python中使用urllib2和urllib模块发送HTTP GET和POST请求的方法,并演示了如何管理和使用Cookie。

urllib2和urllib是Python中的两个内置模块,要实现HTTP功能,实现方式是以urllib2为主,urllib为辅

urllib2提供一个基础函数urlopen,通过向指定的url发出请求来获取数据。最简单的形式是

get 请求

import urllib2

url = "http://www.zhihu.com"

#请求
request = urllib2.Request(url,headers={})
#响应
response = urllib2.urlopen(request)

html = response.read()
#将下载的内容保存到zhihu.html中
with open('zhihu.html','wb') as f:
f.write(html)

post请求

import urllib
import urllib2

url = 'https://passport.youkuaiyun.com/account/verify'

data = {'username': '******', 'password': '*****', 'lt': "LT-24129-fLph1VM5RqhCgq4OdpuReGFC0p5Hbh"}

#user-gent和referer是写入头信息
#user-agent,有些服务器或proxy会通过改值来判断是否是浏览器发出的请求

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'

#referer服务器有时候会检查防盗链
referer = 'https://passport.youkuaiyun.com'

data = urllib.urlencode(data)

try:

request = urllib2.Request(url)

request.add_header('User-agent', user_agent)
request.add_header('Referer', referer)
request.add_data(data)

response = urllib2.urlopen(request)

print response.read()

except urllib2.HTTPError, e:

print e.code

3:Cookie处理

urllib2对Cookie的处理也是自动的,使用Cookiejar函数进行cookie管理
import urllib2
import cookielib

cookie = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

response = opener.open('https://www.zhihu.com')
for item in cookie:

print item.name+':'+item.value








转载于:https://www.cnblogs.com/paulversion/p/8304238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值