好久不更新博客了。。。

之前的博文都是通过urllib2进行http访问,接下来我要说一个利器啊!requests模块,无法用语言对他进行赞扬了,需要的,有兴趣的,可以去了解下,移步官方中文文档:

Requests: 让 HTTP 服务人类

简直是不要太刁。。。

这篇博文呢,主要是将之前博文中用urllib2写的HttpClient类换成request。代码如下:

# coding=utf-8
from __future__ import unicode_literals
import requests
from io import StringIO class HttpClient:
def __init__(self):
pass
__headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Host':'www.xiami.com'
}
__proxies = {
# "http": "http://10.10.1.10:3128",
# "https": "http://10.10.1.10:1080",
} def get(self, url, params=None, retries=3):
try:
req = requests.get(url, headers=self.__headers, timeout=30, params=params,
proxies=self.__proxies)
req.raise_for_status()
return req.text
except Exception,e:
print e
if retries > 0:
return self.get(url, params, retries - 1)
else:
print "Get Failed", url
return '' def post(self, url, data=None, retires=3):
try:
req = requests.post(url, headers=self.__headers, timeout=30, data=data,
proxies=self.__proxies)
req.raise_for_status()
return req.text
except Exception,e:
print e
if retires > 0:
return self.post(url,data,retires - 1)
else:
print "Post Failed", url
return '' def download(self, url, file_name, params=None, cookies=None):
try:
req = requests.get(url, headers=self.__headers, params=params,
proxies=self.__proxies)
output = open(file_name, 'wb')
output.write(req.content)
output.close()
except Exception,e:
print 'error',e def get_cookies(self, url, key, params=None):
try:
req = requests.get(url, headers=self.__headers, timeout=30, params=params,
proxies=self.__proxies)
req.raise_for_status()
return req.cookies.get(key,'')
except Exception,e:
return '' def get_headers(self, url, key, params=None):
try:
req = requests.get(url, headers=self.__headers, timeout=30, params=params,
proxies=self.__proxies)
req.raise_for_status()
return req.headers.get(key)
except Exception,e:
return ''

  记录一下,后面会时常更新博文的。

更新换代之requests库的更多相关文章

  1. Python爬虫小白入门(二)requests库

    一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...

  2. Requests库上传文件时UnicodeDecodeError: 'ascii' codec can't decode byte错误解析

    在使用Request上传文件的时候碰到如下错误提示: 2013-12-20 20:51:09,235 __main__ ERROR 'ascii' codec can't decode byte 0x ...

  3. Requests库的几种请求 - 通过API操作Github

    本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...

  4. python脚本实例002- 利用requests库实现应用登录

    #! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...

  5. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  6. python WEB接口自动化测试之requests库详解

    由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...

  7. python爬虫从入门到放弃(四)之 Requests库的基本使用

    什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...

  8. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  9. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

随机推荐

  1. hadoop balancer 二

    1.每次迭代一个datanode会移动不超过如下两者中较小的一个的数据量 1)10G 2)能力阈值 dfs.datanode.balance.bandwidthPerSec 每次迭代时间不会超过20分 ...

  2. D - Guess UVALive - 4255 拓扑排序

    Given a sequence of integers, a1, a2, . . . , an, we define its sign matrix S such that, for 1 ≤ i ≤ ...

  3. FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration

    添加spring-context-support的依赖到POM: <!-- spring-context-support --> <!-- https://mvnrepository ...

  4. linux top进程状态D

    什么是D状态 运行在KVM虚拟机里的一些进程突然出了问题,这些出了问题的进程无法用kill杀掉,使用ps可以看到这些进程处于D状态: [build@kbuild-john ~]$ ps -a -o p ...

  5. mysql模糊查询语句

    select * from tbl_actor where first_char like 'p%' order by first_char;

  6. Android 自己定义控件实现刮刮卡效果 真的就仅仅是刮刮卡么

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40162163 , 本文出自:[张鸿洋的博客] 非常久以前也过一个html5的刮刮 ...

  7. 转:Redis 缓存策略

    转:http://api.crap.cn/index.do#/web/article/detail/web/ARTICLE/7754a002-6400-442d-8dc8-e76e72d948ac 目 ...

  8. zoj 1880 - Tug of War

    题目:有n个人分成两组,两组人数差不能超过1,找到两组的人重量之差的最小值. 分析:dp,二维01背包. 由于必须放在两个组中的一组,直接背包全部可到状态, 取出相差不超过 1的最接近 sum/2的值 ...

  9. [LeetCode][Java] 4Sum

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  10. How to: Set Properties of Web Application Projects

    https://msdn.microsoft.com/library/aa983454(v=vs.100).aspx ASP.NET Web application projects share th ...