Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4468624
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2012-05-02 04:53:46

httplib2 对cookie的支持不是很好,虽然可以手动设定,但是不够自动化。
官方文档里面的POST之后的: headers['Cookie'] = resp['set-cookie']
但是我测试设置之后的get里面cookie不是设置的,而是每次都改变,也就是说需要把 headers['Cookie']
设置成为一个合法的内容才行,我是听包然后设置的,是没有问题的。

所以技术选择python3自带的urllib。

1. 代理和cookie的设置

点击(此处)折叠或打开

  1. import urllib.request, urllib.error, urllib.parse
  2. import urllib.response
  3. import http.cookiejar
  4. import gzip
  5. import io

  6. # proxy_cookie setting:
  7. proxy_support = urllib.request.ProxyHandler({'http': ''})
  8. cookie_support= urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())

  9. opener = urllib.request.build_opener(proxy_support, cookie_support, urllib.request.HTTPHandler)
  10. urllib.request.install_opener(opener)

  11. content = urllib.request.urlopen('').read()
2. 自动压缩的设置

点击(此处)折叠或打开

  1. # deflate support
  2. def deflate(data):
  3.     """ zlib only provides the zlib compress format, not the deflate format;
  4.         so on top of all there's this workaround:
  5.     """
  6.     import zlib
  7.     try:
  8.         return zlib.decompress(data, -zlib.MAX_WBITS)
  9.     except zlib.error:
  10.         return zlib.decompress(data)


  11. class ContentEncodingProcessor(urllib.request.BaseHandler):
  12.     """ A handler to add gzip capabilities to urllib.request """
  13.  


  14.     def http_request(self, req):
  15.         ''' add headers to requests
  16.         '''
  17.         req.add_header("Accept-Encoding", "gzip, deflate")
  18.         return req
  19.  
  20.     def http_response(self, req, resp):
  21.         ''' decode '''
  22.         old_resp = resp
  23.         # gzip
  24.         if resp.headers.get("content-encoding") == "gzip":
  25.             gz = gzip.GzipFile(
  26.                     fileobj = io.BytesIO( resp.read() ),
  27.                     mode="r"
  28.                     )
  29.             resp = urllib.response.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
  30.             resp.msg = old_resp.msg
  31.         # deflate
  32.         if resp.headers.get("content-encoding") == "deflate":
  33.             gz = io.BytesIO( deflate(resp.read()) )
  34.             resp = urllib.response.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)  # class to add info() and geturl
  35.             resp.msg = old_resp.msg
  36.         return resp

调用时十分方便:
encoding_support = ContentEncodingProcessor
opener = urllib.request.build_opener( encoding_support, urllib.request.HTTPHandler )
 
#直接用opener打开网页,如果服务器支持gzip/defalte则自动解压缩
content = opener.open('').read()


阅读(2540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~