requests(爬虫常用)库的使用
Requests库的使用
基于urllib改写的库
示例:
import requests
response=requests.get('http://www.baidu.com')#get请求
print(response.status_code,response.url,response.cookies,response.text,sep='\n')
import requests
response=requests.post('http://httpbin.org/post')#post请求
print(response.text)#.text得到的都是字符串类型的值
带参数的get请求
import requests
data={
'name':'abc',
'age':15
}
response=requests.get('http://httpbin.org/get',params=data)#post的参数请求时data=data
print(response.text) ----------------------------------------------
#或者直接将参数拼接在url上
import requests
response=requests.get('http://httpbin.org/get?name=adas&age=12')
print(response.text)
将返回的结果变为json格式
import requests
import json
response=requests.get('http://httpbin.org/get')
print(response.json())
print(json.loads(response.text))#等价于上面的
获取二进制数据
import requests
response=requests.get('https://weibo.com/favicon.ico')
print(response.content)
with open('weibo.ico','wb')as f:
f.write(response.content)
添加http的headers属性
import requests
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
response=requests.get('https://zhihu.com/',headers=headers)
print(response.status_code)
print(response.text)
带参数的post请求
import requests
data={
'name':'wang',
'age':88
}
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
response=requests.post('http://httpbin.org/post',data=data,headers=headers)
print(response.text)
response的一些属性
import requests
response=requests.get('http://www.baidu.com')
print(response.headers)
print(response.text)
print(response.status_code)
print(response.content)
print(response.cookies)
#...还有就省略了
response状态码的判断
import requests
response=requests.get('http://www.baidu.com')
# if response.status_code==requests.codes.ok:
if response.status_code==200:
print('ok')
else:
print('error')
文件上传
import requests
files={
'file':open('weibo.ico','rb')
}
response=requests.post('http://httpbin.org/post',files=files)
print(response.text)
获取cookie
import requests
response=requests.get('htt
cook=response.cookies
print(type(cook))
for key,value in cook.item
print(key,'=',value)
维持会话
模拟登陆
import requests
s=requests.Session()#声明session对象,通过session请求网站
s.get('http://httpbin.org/cookies/set/name/123')
response=s.get('http://httpbin.org/cookies')
print(response.text)
证书验证
import requests
from requests.packages import urllib3
urllib3.disable_warnings()#去除py警告
response=requests.get('https://www.12306.cn',verify=False)#去除证书验证
print(response.status_code)
代理ip
import requests
proxies={
'http': 'http://47.89.10.103:80/'
}
response=requests.get('http://www.geogle.com',proxies=proxies)
print(response.text)
有密码的代理ip
import requests
proxies={
'http':'http://user.password@47.89.10.103:80'
}
response=requests.get('http://www.geogle.com',proxies=proxies)
print(response.text)
超时设置
import requests
from requests.exceptions import ReadTimeout
try:
response=requests.get('https://taobao.com',timeout=0.1)
except ReadTimeout as e:
print('timeout')
认证设置(需要直接登陆才能查看网站)
import requests
from requests import HTTPBasicAuth
response=requests.get('http://115.44.48.789:8888',auth=HTTPBasicAuth('user',''))
print(response.status_code)
异常处理
import requests
from requests import ReadTimeout,ConnectionError,RequestException
try:
response=requests.get('http://www.baidu.com',timeout=0.5)
print(response.status_code)
except ReadTimeout:
print('timeout')
except ConnectionError:
print('connectionerror')
except RequestException:
print('requesterror')
requests(爬虫常用)库的使用的更多相关文章
- 爬虫-Python爬虫常用库
一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...
- Python爬虫学习==>第五章:爬虫常用库的安装
学习目的: 爬虫有请求库(request.selenium).解析库.存储库(MongoDB.Redis).工具库,此节学习安装常用库的安装 正式步骤 Step1:urllib和re库 这两个库在安装 ...
- Python爬虫常用库安装
建议更换pip源到国内镜像,下载会快很多:https://www.cnblogs.com/believepd/p/10499844.html requests pip3 install request ...
- python爬虫常用库和安装 -- windows7环境
1:urllib python自带 2:re python自带 3:requests pip install requests 4:selenium 需要依赖chrome ...
- 爬虫常用库之pyquery 库
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的.他的官方文档地址是:http://packages. ...
- Python 爬虫常用库(九)
- [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍
[根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...
- [python爬虫]Requests-BeautifulSoup-Re库方案--robots协议与Requests库实战
[根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 慕课链接:https://www.icourse163.org/learn/BIT-1001870001?tid=100223 ...
- Python 爬虫常用的库
一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...
- $python爬虫系列(2)—— requests和BeautifulSoup库的基本用法
本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...
随机推荐
- ogre3D学习基础10 -- 键盘控制与鼠标控制(缓冲控制)
带缓冲的鼠标.键盘操作,这里的鼠标.按键事件会被各自的监听器捕获.其中OIS中定义的两个类MouseListener,KeyListener负责对事件的处理.我们需要使用这两个类的接口. 当一个键被按 ...
- webdriver高级应用- 使用日志模块记录测试过程中的信息
在自动化脚本执行过程中,使用Python的日志模块记录在测试用例执行过程中一些重要信息或者错误日志等,用于监控和后续调试脚本. 在pycharm下新建工程,并创建Log.py.Logger.conf以 ...
- 缓存淘汰算法之FIFO
前段时间去网易面试,被这个问题卡住,先做总结如下: 常用缓存淘汰算法 FIFO类:First In First Out,先进先出.判断被存储的时间,离目前最远的数据优先被淘汰. LRU类:Least ...
- activemq的安装启动
Activemq安装和启动 官网:http://activemq.apache.org/ 安装启动: $ tar -zxvf apache-activemq-5.11.1-bin.tar.gz ...
- hihoCoder #1246 王胖浩与环
题目大意 $n$($1\le n\le 2000$)个正整数 $a_1, a_2, \dots, a_n$($a_i\le 5\times 10^7$)分布在一个圆环上. 定义 $b_k$ 为:将环上 ...
- VK Cup 2016 - Qualification Round 1——A. Voting for Photos(queue+map)
A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [USACO Section 4.4]追查坏牛奶Pollutant Control (最小割)
题目链接 Solution 一眼看过去就是最小割,但是要求割边最少的最小的割. 所以要用骚操作... 建边的时候每条边权 \(w = w * (E+1) + 1;\) 那么这样建图跑出来的 \(max ...
- 【CCF】有趣的数 数位dp
[思路] dp[i][j]表示前i个数为第j种状态,考虑6种状态 0: 出现且仅出现 2 1: 出现且仅出现 2 0 2: 出现且仅出现 2 3 3: 出现且仅出现 2 0 1 4: 出现且仅出现 2 ...
- inux读取ISO文件或是光驱的方法--挂载
inux读取ISO文件或是光驱的方法--挂载 首先在虚拟机选项的设置里设置CD/DVD选项,勾选:Connect at power on 再在连接中选择:Use ISO image file即选择镜像 ...
- 需要打印真实尺寸大小等需求的,css的单位可以使用mm等做单位
今天甲方那边改需求了,要求打印出来的尺寸是85mm/55mm的,开始还一直在网上找px和mm的相关换算,结果去w3c看了,竟然还有mm单位的, 在这里做个笔记