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(爬虫常用)库的使用的更多相关文章

  1. 爬虫-Python爬虫常用库

    一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...

  2. Python爬虫学习==>第五章:爬虫常用库的安装

    学习目的: 爬虫有请求库(request.selenium).解析库.存储库(MongoDB.Redis).工具库,此节学习安装常用库的安装 正式步骤 Step1:urllib和re库 这两个库在安装 ...

  3. Python爬虫常用库安装

    建议更换pip源到国内镜像,下载会快很多:https://www.cnblogs.com/believepd/p/10499844.html requests pip3 install request ...

  4. python爬虫常用库和安装 -- windows7环境

    1:urllib  python自带 2:re      python自带 3:requests     pip install requests 4:selenium      需要依赖chrome ...

  5. 爬虫常用库之pyquery 库

    pyquery库是jQuery的Python实现,可以用于解析HTML网页内容,我个人写过的一些抓取网页数据的脚本就是用它来解析html获取数据的.他的官方文档地址是:http://packages. ...

  6. Python 爬虫常用库(九)

  7. [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍

    [根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写  文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...

  8. [python爬虫]Requests-BeautifulSoup-Re库方案--robots协议与Requests库实战

    [根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 慕课链接:https://www.icourse163.org/learn/BIT-1001870001?tid=100223 ...

  9. Python 爬虫常用的库

    一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...

  10. $python爬虫系列(2)—— requests和BeautifulSoup库的基本用法

    本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...

随机推荐

  1. jquery获得iframe内容的高度

    html: <iframe name="rightgp" id="right_frame_h" src="/Poster/rightgp&quo ...

  2. xcode8.1 autolayout 找不到 Update Frames 按钮

  3. selenium - 常用页面操作

    # 2.常用页面操作 # 访问某一个页面url = 'http://www.baidu.com'driver.get(url) # 获取页面的标题title = driver.titleprint(t ...

  4. Thanks for your encourage!

    将近三个月的学习,我的努力换回了代表荣誉的小黄衫,这令我很开心啊...我想是不是要写点什么来表达自己的心情呢=,=  于是就有了以下文字ahhhhhh... 学习心得: (1)学习中总会有失败和成功, ...

  5. kb-01-d<poj3279>--深搜变种,二进制优化;

    poj--3279 题意: 给n*m的矩阵,0 1组成,每次翻转一个格子可以将上下左右的五个节点翻转,求,把所有的格子翻转成0:输出每个个字的翻转次数:最少字数: 做法: 从上到下,第一行翻转的情况确 ...

  6. 如何在 Windows 上 使用 ONLYOFFICE 协作编辑文档

    ONLYOFFICE Document Server提供文档协作的服务功能,支持Word,Excel和PowerPoint的协作.但是这里告诉我们,需要进行文档管理和存储的二次开发. Please n ...

  7. getBoundingClientRect说明

    getBoundingClientRect用于获取某个元素相对于视窗的位置集合. 1.语法:这个方法没有参数. rectObject = object.getBoundingClientRect() ...

  8. Java 线程池的原理与实现学习(三)

    一简介 线程的使用在java中占有极其重要的地位,jdk1.4及其之前的jdk版本,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观,Jdk1.5之后加入了java.util.c ...

  9. POJ3539 Elevator

    Time Limit: 4000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Edward wor ...

  10. Yii使用find findAll查找出指定字段的实现方法

    Yii使用find findAll查找出指定字段的实现方法,非常实用的技巧,需要的朋友可以参考下. 用过Yii的朋友都知道,采用如下方法: 查看代码   打印 1 modelName::model() ...