好久不更新博客了。。。

之前的博文都是通过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. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  2. mySQL and sqoop for ubuntu

    数据的导入导出 ——MySQL & sqoop in Ubuntu 1.完成搭建hadoop集群 2.安装MySQL sudo apt-get install mysql-server mys ...

  3. [luoguP1433] 吃奶酪(DP || Dfs)

    传送门 深搜加剪纸可A(O(玄学) 1274ms) ——代码 #include <cmath> #include <cstdio> #include <iostream& ...

  4. FLASH BACK

    overview of different flashback technologies flashback query(including flashback query, flashback ve ...

  5. 1. 数组之差TapeEquilibrium Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.

    数组之差 package com.code; public class Test03_3 { public static int solution(int[] A) { int size = A.le ...

  6. python基础练习-猜年龄、编写登陆接口小程序

    python基础练习:   一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...

  7. BestCoder Round #61 (div.2) C.Subtrees dfs

    Subtrees   问题描述 一棵有N个节点的完全二叉树,问有多少种子树所包含的节点数量不同. 输入描述 输入有多组数据,不超过1000组. 每组数据输入一行包含一个整数N.(1\leq N\leq ...

  8. C 符号表导出

    编译符号表导出示例: 使用GCC编译链接参数--version-script 控制动态符号表,如想 使用链接参数 --retain-symbols-file 控制静态符号表,--version-scr ...

  9. C# 学习笔记 三层架构系列(控件一)

    下面是我两周的学习总结:这是我写给自己的,如果哪位朋友有幸看到这篇文章就是缘分.如果所说的内容不对,就请纠正.勿喷!!! 想要将两周的学习知识通过文字.通过代码.通过图片储备起来,以防自己那天思维短路 ...

  10. 【Bzoj2260】【Bzoj4349】商店购物 & 最小树形图

    目录 List Bzoj 2260 商店购物 Description Input Output Sample Input Sample Output Bzoj 4349 最小树形图 Descripti ...