好久不更新博客了。。。

之前的博文都是通过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. [bzoj1833][ZJOI2010][count] (数位dp)

    Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a.b,含义如上所述. Output 输出文 ...

  2. phpcms 短信替换

    后台表单向导文件路径: [/www/wwwroot/phpcms/phpcms/modules/formguide/templates/formguide_info_list.tpl.php] pub ...

  3. [luoguP1316] 丢瓶盖(二分答案)

    传送门 二分答案再判断即可 ——代码 #include <cstdio> #include <iostream> #include <algorithm> #def ...

  4. hdu 2545 并查集 树上战争

    #include<stdio.h> #include<string.h> #define N 110000 struct node {     int father,count ...

  5. Springmvc 一个简单的管理系统 我所遇到的坑1(持续更新)

    前言 好久没有用springmvc写项目了,抽时间写一个简单的springmvc项目 是什么(what)为什么(why)怎么做(how) 1.读书破万卷下笔如有神(理清思路,知识储备和前期整理) 2. ...

  6. C++ new malloc realloc

    int* a = new int;          分配了存储空间,但没有赋初值 int* a = new int(10)     分配了存储空间,并赋初值,即*a = 10 int* a = ne ...

  7. 最全Pycharm教程(37)——Pycharm版本号控制之基础篇

    1.主题 介绍Pycharm的版本号控制系统 2.准备工作 (1)Pycharm版本号为2.7或者更高 (2)已经创建一个project.參见Getting Started tutorial (3)安 ...

  8. I/O虚拟化

    note:这里主要记录我对IO虚拟化的理解,希望这篇文章对想了解虚拟化IO的同学有点帮助.这是我在看论文[vale,a switched ethernet for virtual machines]的 ...

  9. 工作总结 c#如何将两个List集合合并

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. leetcode 217 Contains Duplicate 数组中是否有反复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...