Python爬虫学习笔记-2.Requests库
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库的更多相关文章
- Python爬虫学习笔记-1.Urllib库
urllib 是python内置的基本库,提供了一系列用于操作URL的功能,我们可以通过它来做一个简单的爬虫. 0X01 基本使用 简单的爬取一个页面: import urllib2 request ...
- python爬虫学习,使用requests库来实现模拟登录4399小游戏网站。
1.首先分析请求,打开4399网站. 右键检查元素或者F12打开开发者工具.然后找到network选项, 这里最好勾选perserve log 选项,用来保存请求日志.这时我们来先用我们的账号密码登陆 ...
- Python爬虫利器一之Requests库的用法
前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...
- python爬虫学习(6) —— 神器 Requests
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
- Python学习笔记之——requests库
requests库一个优雅而简单的用于Python的HTTP库,可以极大的简化我们发送http请求及获取响应的代码. requests是python的第三方库,所以使用之前需要先安装. 1.安装之后就 ...
- python爬虫学习笔记
爬虫的分类 1.通用爬虫:通用爬虫是搜索引擎(Baidu.Google.Yahoo等)“抓取系统”的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 简单来讲就是尽可 ...
- python爬虫学习(一):BeautifulSoup库基础及一般元素提取方法
最近在看爬虫相关的东西,一方面是兴趣,另一方面也是借学习爬虫练习python的使用,推荐一个很好的入门教程:中国大学MOOC的<python网络爬虫与信息提取>,是由北京理工的副教授嵩天老 ...
随机推荐
- arduino入门套件学习过程-安装配置
arduino作为非常火的开源软硬件,有其巨大的优势和前景. 我呢,近期正好需要用arduino做一些小东西,借此机会,分享我的arduino学习历程,欢迎各位朋友一起讨论,指点! 现在进入正题: 这 ...
- SpringCloud微服务部署
https://blog.csdn.net/weixin_36397925/article/details/79496657 https://blog.csdn.net/forezp/article/ ...
- tensorflow prelu的实现细节
tensorflow prelu的实现细节 output = tf.nn.leaky_relu(input, alpha=tf_gamma_data,name=name) #tf.nn.leaky_r ...
- c语言for循环等语句详解
循环结构有: . goto语句和if语句构成循环 .while语句 .do-while语句 .for语句 goto语句 goto语句是一种无条件转移语句, 与Basic中的goto语句相似.goto语 ...
- Json---Linux下使用Jsoncpp
一.安装 scons 下载地址:http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download ...
- 安卓横竖屏切换时activity的生命周期
关于Activity横竖屏切换的声明周期变化: 1.新建一个Activity并把各个生命周期打印出来 2.运行Activity,得到如下信息 onCreate-->onStart-->on ...
- update-alternatives常用命令(转自http://blog.csdn.net/baggio1006/article/details/6338623)
Linux 发展到今天,可用的软件已经非常多了.这样自然会有一些软件的功能大致上相同.例如,同样是编辑器,就有 nvi.vim.emacs.nano,而且我说的这些还只是一部分.大多数情况下,这样的功 ...
- 我的openwrt开发相关文章
openwrt学习笔记: 在openwrt的学习过程中,走了非常多的弯路.一直以来有个期盼.希望能够出个简易教程,希望openwrt的同仁们能够更加高速的入手. . openwrt学习笔记(三十二): ...
- 转载:AOP那点事
原作者:黄勇 博客地址:https://my.oschina.net/huangyong/blog/161338 又是一个周末,刚给宝宝喂完牛奶,终于让她睡着了.所以现在我才能腾出手来,坐在电脑面前给 ...
- CentOS7 防火墙配置(关闭)
CentOS7 的防火墙配置跟曾经版本号有非常大差别,经过大量尝试,最终找到解决这个问题的关键 CentOS7这个版本号的防火墙默认使用的是firewall.与之前的版本号使用iptables不一样. ...