好久不更新博客了。。。

之前的博文都是通过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. BZOJ 3993 Luogu P3324 [SDOI2015]星际战争 (最大流、二分答案)

    字符串终于告一段落了! 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3993 (luogu) https://www.l ...

  2. ZOJ 3684 Destroy

    Destroy Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...

  3. nyoj 95 众数问题(set)

    众数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重 ...

  4. POJ2528 Uva10587 Mayor's posters

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  5. [poj2975]Nim_博弈论

    Nim poj-2975 题目大意:给定n堆石子,问:多少堆石子满足操作之后先手必胜. 注释:$1\le n\le 10^3$. 想法: 我们设M=sg(x1)^sg(x2)^...^sg(xn).其 ...

  6. python_swift_project_middleware

    1. 写openstack swift的middleware 首先要确定swift用的是哪个middleware文件. 比如healthcheck这个中间件,在机器上有很多同名文件如下, 这两行可能是 ...

  7. 第5章 Cisco测试命令和TCP/IP连接故障处理

    第5章 Cisco测试命令和TCP/IP连接故障处理 一.故障处理命令 1.show命令: 1) 全局命令: show version :显示系统硬件和软件版本.DRAM.Flash show sta ...

  8. Sping框架的IOC特性

    IOC(Inversion of Control):控制反转 以下以课程与老师的安排来介绍控制反转. 一个合理的课程编排系统应该围绕培训的内容为核心,而不应该以具体的培训老师为核心,这样才能在正常授课 ...

  9. Cisco VPP(1) 简单介绍

    一.简单介绍 VPP全称Vector Packet Processing.是Cisco2002年开发的商用代码. 2016年2月11号,Linux基金会创建FD.io项目.Cisco将VPP代码的开源 ...

  10. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...