• 基本Get请求:
#-*- coding:utf-8 -*-
import requests
url = 'http://www.baidu.com'
r = requests.get(url)
print r.text

  • 带参数Get请求:
#-*- coding:utf-8 -*-
import requests
url = 'http://www.baidu.com'
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(url, params=payload)
print r.text
  • POST请求模拟登陆及一些返回对象的方法:
#-*- coding:utf-8 -*-
import requests
url1 = 'http://www.exanple.com/login'#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers) print res2.content#获得二进制响应内容
print res2.raw#获得原始响应内容,需要stream=True
print res2.raw.read(50)
print type(res2.text)#返回解码成unicode的内容
print res2.url
print res2.history#追踪重定向
print res2.cookies
print res2.cookies['example_cookie_name']
print res2.headers
print res2.headers['Content-Type']
print res2.headers.get('content-type')
print res2.json#讲返回内容编码为json
print res2.encoding#返回内容编码
print res2.status_code#返回http状态码
print res2.raise_for_status()#返回错误状态码

  • 使用Session()对象的写法(Prepared Requests):
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = 'http://www.exanple.com/login'#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
} prepped1 = requests.Request('POST', url1,
data=data,
headers=headers
).prepare()
s.send(prepped1) '''
也可以这样写
res = requests.Request('POST', url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
''' prepare2 = requests.Request('POST', url2,
headers=headers
).prepare()
res2 = s.send(prepare2) print res2.content
  • 另一种写法 :
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = 'http://www.exanple.com/login'#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
res1 = s.post(url1, data=data)
res2 = s.post(url2)
print(resp2.content)

SessionApi

  • 其他的一些请求方式
>>> 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")

遇到的问题:


在cmd下执行,遇到个小错误:

UnicodeEncodeError:'gbk' codec can't encode character u'\xbb' in
position 23460: illegal multibyte sequence

分析:
1、Unicode是编码还是解码

UnicodeEncodeError

很明显是在编码的时候出现了错误

2、用了什么编码

'gbk' codec can't encode character

使用GBK编码出错

解决办法:

确定当前字符串,比如

#-*- coding:utf-8 -*-
import requests
url = 'http://www.baidu.com'
r = requests.get(url)
print r.encoding
>utf-8

已经确定html的字符串是utf-8的,则可以直接去通过utf-8去编码。

print r.text.encode('utf-8')

作者:Jelvis
链接:http://www.jianshu.com/p/e1f8b690b951
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

利用Requests库写爬虫的更多相关文章

  1. requests库写接口测试框架初学习

    学习网址:    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dscpm/ff75b907-415d-4220-89 ...

  2. 爬虫入门实例:利用requests库爬取笔趣小说网

    w3cschool上的来练练手,爬取笔趣看小说http://www.biqukan.com/, 爬取<凡人修仙传仙界篇>的所有章节 1.利用requests访问目标网址,使用了get方法 ...

  3. Requests库网络爬虫实战

    实例一:页面的爬取 >>> import requests>>> r= requests.get("https://item.jd.com/1000037 ...

  4. python利用requests库模拟post请求时json的使用

    我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...

  5. requests库(爬虫)

    北京理工大学嵩天老师的课程:http://www.icourse163.org/course/BIT-1001870001 官方文档:http://docs.python-requests.org/e ...

  6. python脚本实例002- 利用requests库实现应用登录

    #! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...

  7. 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)

    一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...

  8. Python爬虫:HTTP协议、Requests库(爬虫学习第一天)

    HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...

  9. 利用requests库访问网站

    1.关于requests库 函数 Response对象包含服务器返回的所有信息,也包含请求的Request信息. 访问百度二十次 import requests def getHTMLText(url ...

随机推荐

  1. OCR论文整理

    论文地址:https://github.com/ChanChiChoi/awesome-ocr 下面是已经看过的论文: CTPN CRNN TextBoxes EAST FOTS PixelLink

  2. linux Git版本控制学习与Git服务器搭建

    来源地址 要随时掌握工作区的状态,使用git status命令. 如果git status告诉你有文件被修改过,用git diff可以查看修改内容. 初始化一个Git仓库,使用git init命令. ...

  3. SSH框架测试

    1.struts的测试:目的是能否正确显示页面. 流程如图: 2.spring测试:目的是能否得到bean 3.Hibernate测试:目的是能否跟数据库操作,测试事务 事务测试: 这个要向数据库中保 ...

  4. 解决Maven提示:Could not read settings.xml

    在Eclipse中配置maven时,提示错误:Could not read settings.xml.用户配置无法生效. 1.首先检查自己的settings.xml配置文件,发现在<!----& ...

  5. Python 字符串前面加u,r,b的含义

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时 ...

  6. python中高阶函数与装饰器(3)

    >>> f = lambda x: x * x>>> f<function <lambda> at 0x101c6ef28> >> ...

  7. 最新的IDEA激活方式

    IntelliJ IDEA2017.3 激活 转自:http://blog.csdn.net/zx110503/article/details/78734428 最新的IDEA激活方式 使用网上传统的 ...

  8. Redis学习一:Nosql入门和概述

    现在Redis越来越火,为了适应技术的发展,开始学习一下Redis,在学习Redis之前先学习一下Nosql. 第一部分:入门概述 1.1 互联网时代背景下大机遇,为什么用nosql 1.1.1 单机 ...

  9. util.promisify 的那些事儿

    util.promisify是在node.js 8.x版本中新增的一个工具,用于将老式的Error first callback转换为Promise对象,让老项目改造变得更为轻松. 在官方推出这个工具 ...

  10. 使用 TypeScript 改造构建工具及测试用例

    最近的一段时间一直在搞TypeScript,一个巨硬出品.赋予JavaScript语言静态类型和编译的语言. 第一个完全使用TypeScript重构的纯Node.js项目已经上线并稳定运行了. 第二个 ...