简介

#介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3)

#注意:requests库发送请求将网页内容下载下来以后,并不会执行js代码,这需要我们自己分析目标站点然后发起新的request请求

#安装:pip3 install requests

#各种请求方式:常用的就是requests.get()和requests.post()
>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})
>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
>>> r = requests.delete('http://httpbin.org/delete')
>>> r = requests.head('http://httpbin.org/get')
>>> r = requests.options('http://httpbin.org/get')

1、GET请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1、无参数实例
 
import requests
 
ret = requests.get('https://github.com/timeline.json')
 
print ret.url
print ret.text
 
 
 
# 2、有参数实例
 
import requests
 
payload = {'key1''value1''key2''value2'}
ret = requests.get("http://httpbin.org/get", params=payload)
 
print ret.url
print ret.text

向 https://github.com/timeline.json 发送一个GET请求,将请求和响应相关均封装在 ret 对象中。

2、POST请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1、基本POST实例
 
import requests
 
payload = {'key1''value1''key2''value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
 
print ret.text
 
 
# 2、发送请求头和数据实例
 
import requests
import json
 
url = 'https://api.github.com/some/endpoint'
payload = {'some''data'}
headers = {'content-type''application/json'}
 
ret = requests.post(url, data=json.dumps(payload), headers=headers)
 
print ret.text
print ret.cookies

向https://api.github.com/some/endpoint发送一个POST请求,将请求和相应相关的内容封装在 ret 对象中。

3、其他请求

1
2
3
4
5
6
7
8
9
10
requests.get(url, params=None**kwargs)
requests.post(url, data=None, json=None**kwargs)
requests.put(url, data=None**kwargs)
requests.head(url, **kwargs)
requests.delete(url, **kwargs)
requests.patch(url, data=None**kwargs)
requests.options(url, **kwargs)
 
# 以上方法均是在此方法的基础上构建
requests.request(method, url, **kwargs)

requests模块已经将常用的Http请求方法为用户封装完成,用户直接调用其提供的相应方法即可

Practice

import requests
import re
'''
请求方式:get、post、put…
参数:params、headers、proxies、cookies、data
'''
rsp=requests.get("https://www.hellobi.com/")
ck=requests.utils.dict_from_cookiejar(rsp.cookies)
title=re.compile("<title>(.*?)</title>",re.S).findall(rsp.text)
hd={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"}
px={"http":"http://127.0.0.1:8888",
"https":"http://127.0.0.1:8888",}
rsp=requests.get("https://www.hellobi.com/",proxies=px,headers=hd,cookies=ck)
key={"wd":"韦玮",
}
rsp=requests.get("http://www.baidu.com/s",headers=hd,cookies=ck,params=key)
title=re.compile("<title>(.*?)</title>",re.S).findall(rsp.text) postdata={"name":"测试账号",
"pass":"测试密码"}
rsp=requests.post("http://www.iqianyue.com/mypost/",data=postdata)

  

Python-爬虫-requests的更多相关文章

  1. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  2. Python 爬虫—— requests BeautifulSoup

    本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...

  3. Python爬虫--Requests库

    Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...

  4. 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】

    [写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...

  5. Python爬虫 requests库基础

    requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...

  6. python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例

    第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化... 贴在这里,引以为戒吧. # -*- coding: ...

  7. python爬虫---requests库的用法

    requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...

  8. Python爬虫---requests库快速上手

    一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...

  9. Python爬虫requests判断请求超时并重新发送请求

     下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html  在爬虫的执行当中,总会遇到请求连接 ...

  10. python爬虫——requests库使用代理

    在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...

随机推荐

  1. 随心测试_软测基础_003< 理解测试 >

    目标:对于软件测试基础,利用清晰的框架,掌握相关知识点. 做某件事情,思路如下: 以上过程,理解为:针对x一个对象,围绕特定的目的,利用具备的方法,按一定的流程做事情,并反复思考总结,这样做是否达到目 ...

  2. docker(二) windows10下安装docker

    官方安装文档: https://docs.docker.com/docker-for-windows/install/ https://docs.docker.com/docker-for-windo ...

  3. 解决import模块后提示无此模块的问题

    最近在工作中发现一个奇怪的问题: 明明已经装上了,但是还提示找不到该模块,没办法,我又去site-package文件下面看了: 发现Linux下自带的python2.7里面装上了该模块(我在root用 ...

  4. Linux centos7.5操作系统的安装

    安装centos7.5 1.1 新建虚拟机 1.2 选择客户机系统和版本 1.3 更改虚拟机名称和创建地址.   1.4 选择网络类型 1.5 自定义硬件,选择添加centos7.5镜像 1.6 开机 ...

  5. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  6. 前端cookie操作用到的一些小总结

    前后端完全分离的是目前web开发的大趋势,包括现下流行的前端框架的应用vue,angular,在不同页面跳转时,前端需要对用户登录状态进行判断,拿到用户的id,除了Ajax从服务器端获取数据外,对co ...

  7. Siamese network 孪生神经网络

    Siamese network 孪生神经网络 https://zhuanlan.zhihu.com/p/35040994 https://blog.csdn.net/shenziheng1/artic ...

  8. jmeter笔记(3)--响应结果中文乱码的解决方式

    1.举例 新建HTTP请求访问百度首页,响应结果如下: 2.原因 Jmeter安装目录/bin/jmeter.properties中sampleresult.default.encoding默认为IS ...

  9. Python3 与 C# 并发编程之~ 协程篇

      3.协程篇¶ 去年微信公众号就陆陆续续发布了,我一直以为博客也汇总同步了,这几天有朋友说一直没找到,遂发现,的确是漏了,所以补上一篇 在线预览:https://github.lesschina.c ...

  10. BSGS与扩展BSGS

    BSGS \(BSGS\)算法又称大步小步\((Baby-Step-Giant-Step)\)算法 \(BSGS\)算法主要用于解以下同余方程 \[A^x\equiv B(mod\ p)\]其中\(( ...