requests
>>>import requests
>>> r = requests.get('http://www.zhidaow.com') # 发送请求
>>> r.status_code # 返回码
200
>>> r.headers['content-type'] # 返回头部信息
'text/html; charset=utf8'
>>> r.encoding # 编码信息
'utf-8'
>>> r.text #内容部分(PS,由于编码问题,建议这里使用r.content)
u'<!DOCTYPE html>\n<html xmlns="http://www.w3.org/1999/xhtml"...'
...
>>>import requests
接下来让我们获取一个网页,例如博客的首页:
>>>r = requests.get('http://www.sharejs.com')
接下来,我们就可以使用这个r的各种方法和函数了。
另外,HTTP请求还有很多类型,比如POST,PUT,DELETE,HEAD,OPTIONS。也都可以用同样的方式实现:
>>> r = requests.post("http://httpbin.org/post")
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options("http://httpbin.org/get")
在URLs中传递参数
有时候我们需要在URL中传递参数,比如在采集百度搜索结果时,我们wd参数(搜索词)和rn参数(搜素结果数量),你可以手工组成URL,requests也提供了一种看起来很NB的方法:
>>> payload = {'wd': '张亚楠', 'rn': '100'}
>>> r = requests.get("http://www.baidu.com/s", params=payload)
>>> print r.url
u'http://www.baidu.com/s?rn=100&wd=%E5%BC%A0%E4%BA%9A%E6%A5%A0'
可以通过r.headers来获取响应头内容。
>>>r = requests.get('http://www.zhidaow.com')
>>> r.headers
{
'content-encoding': 'gzip',
'transfer-encoding': 'chunked',
'content-type': 'text/html; charset=utf-8';
...
}
可以看到是以字典的形式返回了全部内容,我们也可以访问部分内容。
>>> r.headers['Content-Type']
'text/html; charset=utf-8'
>>> r.headers.get('content-type')
'text/html; charset=utf-8'
设置超时时间
我们可以通过timeout属性设置超时时间,一旦超过这个时间还没获得响应内容,就会提示错误。
>>> requests.get('http://github.com', timeout=0.001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
理访问
采集时为避免被封IP,经常会使用代理。requests也有相应的proxies属性。
import requests
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://www.zhidaow.com", proxies=proxies)
请求头内容
请求头内容可以用r.request.headers来获取。
>>> r.request.headers
{'Accept-Encoding': 'identity, deflate, compress, gzip',
'Accept': '*/*', 'User-Agent': 'python-requests/1.2.3 CPython/2.7.3 Windows/XP'}
自定义请求头部
伪装请求头部是采集时经常用的,我们可以用这个方法来隐藏:
r = requests.get('http://www.zhidaow.com')
print r.request.headers['User-Agent']
#python-requests/1.2.3 CPython/2.7.3 Windows/XP
headers = {'User-Agent': 'alexkh'}
r = requests.get('http://www.zhidaow.com', headers = headers)
print r.request.headers['User-Agent']
#alexkh
requests的更多相关文章
- requests的content与text导致lxml的解析问题
title: requests的content与text导致lxml的解析问题 date: 2015-04-29 22:49:31 categories: 经验 tags: [Python,lxml, ...
- requests源码阅读学习笔记
0:此文并不想拆requests的功能,目的仅仅只是让自己以后写的代码更pythonic.可能会涉及到一部分requests的功能模块,但全看心情. 1.另一种类的初始化方式 class Reques ...
- Python爬虫小白入门(二)requests库
一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...
- 使用beautifulsoup与requests爬取数据
1.安装需要的库 bs4 beautifulSoup requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...
- python爬虫学习(6) —— 神器 Requests
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...
- ImportError: No module named 'requests'
补充说明: 当前环境是在windows环境下 python版本是:python 3.4. 刚开始学习python,一边看书一边论坛里阅读感兴趣的代码, http://www.oschina.net/c ...
- Python-第三方库requests详解
Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...
- Requests 乱码
当使用Requests请求网页时,出现下面图片中的一些乱码,我就一脸蒙逼. 程序是这样的. def getLinks(articleUrl): headers = { "Uset-Agent ...
- 爬虫requests模块 2
会话对象¶ 会话对象让你能够跨请求保持某些参数.它也会在同一个 Session 实例发出的所有请求之间保持 cookie, 期间使用 urllib3 的 connection pooling 功能.所 ...
- 爬虫requests模块 1
让我们从一些简单的示例开始吧. 发送请求¶ 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试 ...
随机推荐
- 巧用nginx屏蔽对用户不可见的文件
事情的起因是这样的--前端的项目中有一些.less之类的源文件,而为了方便迭代更新发布,直接就把整个工程放到了www目录下. 这样虽然方便了,但是会带来一些安全隐患——用户可以访问/盗取这些源文件. ...
- hdoj-2024
#include "cstdio"#include "cstring"int compare(char s[]);int main(){ int i,n,j; ...
- 2016 - 1 - 20 runloop学习(2)
一:CFRunLoopModeRef 1. CFRunLoopModeRef带表RunLoop的运行模式 2. 一个Runloop可以有若干个mode,每个mode又包含若干个sourse,timer ...
- Reverse Integer ---- LeetCode 007
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...
- Ajax中的eval函数的用法
eval的定义和使用: Eval它是用来计算某个字符串,并且执行其中的JavaScript代码. 语法: 1) eval函数接受一个string这个参数,并且这个参数是必须的,这个参数就是要计算的这个 ...
- Bootstrap全局css
HTML中的所有标题标签,<h1>到<h6>均可使用.另外,还提供了.h1到.h6类,为的是给内联(inline)属性的文本赋予标题的样式.在标题内还可以包含<small ...
- 学习笔记:只有一套app设计稿(5s尺寸)切出4和4s尺寸以及安卓系统主流尺寸的图
如何在只有一套app设计稿(5s尺寸)切出4和4s尺寸以及安卓系统主流尺寸的图 转自:http://www.zhihu.com/question/23255417 版权归原作者所有 目前ios手机 ...
- Asp登陆
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="QYLogin.aspx.c ...
- Runtime 、 Block
1 使用Block方式,对学生对象进行排序. 1.1 问题 在iOS4.0+ 和Mac OS X 10.6+ 中添加了Block概念,以对C语言进行扩展.在Block中可以定义参数列表.返回类型,还可 ...
- OpenFlow Switch学习笔记(一)——基础概念
OpenFlow Switch v1.4.0规范是在2013年10月14号发布,规范涵盖了OpenFlow Switch各个组件的功能定义.Controller与Switch之间的通信协议Open F ...