更新换代之requests库
好久不更新博客了。。。
之前的博文都是通过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库的更多相关文章
- Python爬虫小白入门(二)requests库
一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...
- 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 ...
- Requests库的几种请求 - 通过API操作Github
本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...
- python脚本实例002- 利用requests库实现应用登录
#! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- python爬虫从入门到放弃(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
随机推荐
- UVA - 10825 Anagram and Multiplication
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34594 有一个m位n进制的数,它的特性是这个数依次乘以2,3... ...
- spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)
背景:因为在项目中用到了定时任务,当时想到了spring的quartz,写完发现费了很大功夫,光是整合就花了一上午,其中最大的问题就是版本问题,项目中用的是spring3.2.8的版本,查阅发现,3. ...
- java多线程编程核心技术(一)--多线程技能
1.进程和线程的概念 1.进程:进程是操作系统的基础,是一次程序的执行,是一个程序及其数据在处理机上顺序执行时所发生的活动,是程序在一个数据集合上运行的过程,他是系统进行资源分配和调度的一个独立单位. ...
- Oracle 11.2.0.4.0安装
http://opensgalaxy.com/2015/08/25/oracle11-2-0-4-0%E5%AE%89%E8%A3%85%E5%8F%8A%E8%A1%A5%E4%B8%81%E8%8 ...
- SQL Server死锁总结 [转]
1. 死锁原理 根据操作系统中的定义:死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所站用不会释放的资源而处于的一种永久等待状态. 死锁的四个必要条件:互斥条件(Mutua ...
- zoj——2588 Burning Bridges
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- Hive之单独部署机器
环境说明 CentOS7,hadoop-2.6.5,hive-1.2.2,MariaDB-5.5.60,jdk-1.8 假设hive机已经安装好了MariaDB(已启动且已创建好hive账号,对hiv ...
- [Vue @Component] Load Vue Async Components
Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...
- javaScript 超时与间歇掉用
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- hdu 5078 2014鞍山现场赛 水题
http://acm.hdu.edu.cn/showproblem.php?pid=5078 现场最水的一道题 连排序都不用,由于说了ti<ti+1 //#pragma comment(link ...