整理urllib2库常用语句

class urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])

可以看到,返回对象是个类

This class is an abstraction of a URL request.

data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.

headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments. 

urllib2.urlopen(url[, data][, timeout])

urllib2.urlopen()也可以有data 作为参数,即也可以以post方式打开网页

url, which can be either a string or a Request object.

data的形式urllib2.Request()

  • 单独处理Proxy

之前是小甲鱼有3条语句可以设置代理(是python3了):
结果是得到一个定制的opener,用它替代urlopen()

  1. 列表内容
    proxy_support = urllib.request.ProxyHandler({‘类型(如http)’:‘代理ip : 端口号’})
  2. 定制一个opener
    opener = urllib.request.build_opener(proxy_support)
  3. 安装opener
    改变默认的opener - urlopen()
    urllib.request.install_opener(opener)
    仅调用
    opener.open(url)

单独处理cookie

在《Python爬虫入门六之Cookie的使用》有代码
import cookielib

cookie = cookielib.CookieJar()
handler=urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
#此处的open方法同urllib2的urlopen方法,也可以传入request
response = opener.open('http://www.baidu.com')
for item in cookie:
    print 'Name = '+item.name
    print 'Value = '+item.value
#我觉得和Proxy处理类似,都是先个什么,再handler,再opener,最后用这个opener去打开

同时有post data和cookie怎么搞?

第三个例子算教务处学分绩就有二者了(再看看!)。
cookie就正常来,3行语句搞定一个opener。在urllib2.Request()参数里加上data,然后用opener.open(request)即可

同时有post data 和代理怎么搞?

应该和同时有post data和cookie一样

urllib2.build_opener([handler, …])

Return an OpenerDirector instance, which chains the handlers in the order given. handlers can be either instances of BaseHandler, or subclasses of BaseHandler 

OpenerDirector.open(url[, data][, timeout])

用Proxy,cookies时创建的opener就是OpenerDirector的实例。和urllib2.urlopen()的使用一样

同时有代理和cookie,opener怎么设置?


快捷键

  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值