Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大。

0X01 基本使用

  安装 Requests,只要在你的终端中运行这个简单命令即可:

pip install requests

  基本HTTP 请求类型:

r = requests.get('http://httpbin.org/get')
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")

  简单的一个请求:

import requests
r = requests.get('http://192.168.125.129/config/sql.php?id=1')
print r.headers
print r.status_code
print r.url
print r.text
print r.content

  GET方式:

import requests
payload ={'id':}
r = requests.get('http://192.168.125.129/config/sql.php',params=payload)
print r.url
print r.content

  POST方式:

import requests
payload ={'id':}
r = requests.post('http://192.168.125.129/config/sql.php',data=payload)
print r.content

0X02 高级用法

1、设置headers

import requests
url='http://192.168.125.129/config/sql.php?id=1'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0'}
r= requests.get(url,headers=headers)
print r.text

 2、模拟登录和抓取数据的简单示例

s = requests.session()
data = {'user':'用户名','passdw':'密码'}
#post 换成登录的地址,
res=s.post('http://www.xxx.com/login.php',data);
#换成抓取的地址
s.get('http://www.xxx.com/admin/config.php');

 3、已知cookie,进行登录

import requests
raw_cookies="PHPSESSID=0c1e5a748e064e93e91cca1714708339; security=impossible"
cookies={}
for line in raw_cookies.split(';'):
key,value=line.split('=',)
cookies[key]=value
testurl='http://192.168.125.129/vulnerabilities/upload/'
s=requests.get(testurl,cookies=cookies)
print s.text

 4、SSL证书验证问题

result=requests.get('https://www.v2ex.com', verify=False)

忽略验证SSL证书,不然会报错

  5、302重定向

result=s.post(loginUrl,data=postdata,headers=header,verify=False,allow_redirects=False)

  6、使用Python Requests上传表单数据和文件

import requests
url = "http://www.xxx.cn/upload.php"
files ={"username":(None,"test"),
'filename':('1.jpg',open('1.jpg','rb'),'image/jpeg'),
"password":(None,"test123!")}
res = requests.post(url, files=files)
print res.request.body
print res.request.headers

输出请求体、请求头效果如下:

--5e800fd12507423aa2e4a024db7b1fa1
Content-Disposition: form-data; name="username" test
--5e800fd12507423aa2e4a024db7b1fa1
Content-Disposition: form-data; name="password" test123!
--5e800fd12507423aa2e4a024db7b1fa1
Content-Disposition: form-data; name="filename"; filename="1.jpg"
Content-Type: image/jpeg --5e800fd12507423aa2e4a024db7b1fa1-- {'Content-Length': '', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.12.4', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data; boundary=5e800fd12507423aa2e4a024db7b1fa1'}

 

参考资料:

   http://cn.python-requests.org/zh_CN/latest/user/quickstart.html

Python爬虫学习笔记-2.Requests库的更多相关文章

  1. Python爬虫学习笔记-1.Urllib库

    urllib 是python内置的基本库,提供了一系列用于操作URL的功能,我们可以通过它来做一个简单的爬虫. 0X01 基本使用 简单的爬取一个页面: import urllib2 request ...

  2. python爬虫学习,使用requests库来实现模拟登录4399小游戏网站。

    1.首先分析请求,打开4399网站. 右键检查元素或者F12打开开发者工具.然后找到network选项, 这里最好勾选perserve log 选项,用来保存请求日志.这时我们来先用我们的账号密码登陆 ...

  3. Python爬虫利器一之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...

  4. python爬虫学习(6) —— 神器 Requests

    Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...

  5. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  6. python爬虫学习笔记(一)——环境配置(windows系统)

    在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库)   [推荐地址:清华镜像] https://mirrors ...

  7. Python学习笔记之——requests库

    requests库一个优雅而简单的用于Python的HTTP库,可以极大的简化我们发送http请求及获取响应的代码. requests是python的第三方库,所以使用之前需要先安装. 1.安装之后就 ...

  8. python爬虫学习笔记

    爬虫的分类 1.通用爬虫:通用爬虫是搜索引擎(Baidu.Google.Yahoo等)“抓取系统”的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 简单来讲就是尽可 ...

  9. python爬虫学习(一):BeautifulSoup库基础及一般元素提取方法

    最近在看爬虫相关的东西,一方面是兴趣,另一方面也是借学习爬虫练习python的使用,推荐一个很好的入门教程:中国大学MOOC的<python网络爬虫与信息提取>,是由北京理工的副教授嵩天老 ...

随机推荐

  1. 怎么解决JSP中出现乱码的问题

    首先我们先了解一下问题的原因.一般情况在在每个JSP页的头部都有这样一条语句: 这条语句决定了此页面使用GB2312编码形式,而在数据库中一般用的是iso-8859-1字符集存储数据. 而Java程序 ...

  2. MapReduce 图解流程超详细解答(1)-【map阶段】

    转自:http://www.open-open.com/lib/view/open1453097241308.html 在MapReduce中,一个YARN  应用被称作一个job, MapReduc ...

  3. 关于Unity中的光照(六)

    反射探头 1:镜子金属等具有光滑表面的物体都会反射,而游戏中计算实时反射非常消耗CPU的资源, unity5.0新增了一个反射探头的技术,通过采样点,生成反射Cubemap,然后通过特定的着色器从Cu ...

  4. 解决Ajax跨域问题:Origin http://127.0.0.1:8080 is not allowed by Access-Control-Allow-Origin.

    在服务端上设置一下header,如response.header("Access-Control-Allow-Origin","*");

  5. android下通过app名字打开程序(activity)链接

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00004599 1.手机遥控器模拟快捷键启动app 刚开 ...

  6. 取消excel 工作保护 密码的宏

    Option Explicit Public Sub AllInternalPasswords() ' Breaks worksheet and workbook structure password ...

  7. HttpComponents-Client学习

    HttpComponents-Client 学习 官方文档:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html ...

  8. Java如何处理空堆栈异常?

    在Java编程中,如何处理空堆栈异常? 本例展示了如何使用Date类的System.currentTimeMillis()方法和Stack类的s.empty(),s.pop()方法来处理空堆栈异常. ...

  9. Bind-DLZ with MySQL

    系统环境: 系统:centos 6.8 Mysql: 5.1 BIND: bind--P2.tar.gz IP地址:192.168.153.130 软件下载地址:http://ftp.isc.org/ ...

  10. linux网络配置练习

    查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...