python爬虫requests的使用
1 发送get请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
2 发送post请求获取页面
import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.post(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)
3 伪装浏览器,携带报头
import requests # 伪装我们的报文头,加上Use-Agent 伪装成浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
4 携带数据 (比如 发送请求去登陆)
import requests # 如果伪装登录,可以传送一个字典类型数据
data = {
'''这里放入需要的key:value'''
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
# get请求用params 相当于在url后面拼接key=value&key=value
response = requests.get(url=url,params=data)
# post用data传入参数 携带post的数据
response = requests.post(url=url,data=data)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
5 代理
import requests
# 将代理的服务器放入这里,key为协议类型 value为代理的ip和端口
# 发送https或者http请求会根据不同代理ip选择 为我们发送请求
proxies = {
'http':'http://127.0.0.1:80',
'https':'https://127.0.0.1:80'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,proxies=proxies)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
6 携带cookie
import requests # 如果要带着cookie字典 可以传入cookie,也可以放在报文头当中
cookies = {
#'key':'value',
} # 或者将cookie放在报文头当中
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,cookies=cookies)
#response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)
7 保持session 帮我们保存response中的session
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url)
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
8 设置连接超时时间
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,timeout = 3) # 3秒时间为超时时间
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
9 设置ssl校验 对方https协议合法性是否忽略
import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,verify=False) # 不校验ssl 如果对方https协议不合法,我们忽略 继续请求
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)
10 重新连接次数
import requests
from retrying import retry @retry(stop_max_attempt_number=3) # 设置超时重新连接 次数3
def get( url ):
response = requests.get(url=url,timeout=3)
return response.content.decode() url = 'http://www.baidu.com'
html = get(url)
print(html)
python爬虫requests的使用的更多相关文章
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- Python 爬虫—— requests BeautifulSoup
本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...
- Python爬虫--Requests库
Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
- python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例
第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化... 贴在这里,引以为戒吧. # -*- coding: ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- Python爬虫requests判断请求超时并重新发送请求
下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html 在爬虫的执行当中,总会遇到请求连接 ...
- python爬虫——requests库使用代理
在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...
随机推荐
- vmware虚拟机各个版本的安装破解(附安装包和注册机)
VMware 是平时我们常用的虚拟机软件,特别是我们平时想试试其他的系统,比如说linux系统的时候但是又不想安装双系统,那么这个时候我们就可以试试这款虚拟软 件,如果你的电脑配置(主要是内存)够好的 ...
- Unity3D判断角色对敌人是否可见
在编写敌人AI的状态机时 经常需要判断角色对敌人来说是不是可见的 如果是可见的,则做出追击或者攻击动作 如果是不可见的,则保持idle或者巡逻状态 判断是否可见 包括两个步骤 1.地方角色的视见向量和 ...
- canvas 绘制图形
canvas 绘制图形: 注意: canvas 的宽高设置在行内,否则会使画布(canvas)产生扭曲,绘图变形: <!DOCTYPE html> <html lang=" ...
- IPFS家族(一)
IPFS这个项目其实很大,并不像大家想象的是一个东西,IPFS是由很多模块组成,每一个模块现在都已经独立成项目了,并且有自己的主页.让我们来简单看一下IPFS家族成员. 协议实验室的主页:https: ...
- 【基础】这15种CSS居中的方式,你都用过哪几种?
简言 CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案汇编整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是 ...
- Spring Boot Junit单元测试
http://blog.csdn.net/catoop/article/details/50752964
- JSON Web Token - 在Web应用间安全地传递信息
转载自:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使 ...
- SpringAOP-JDK 动态代理和 CGLIB 代理
在 Spring 中 AOP 代理使用 JDK 动态代理和 CGLIB 代理来实现,默认如果目标对象是接口,则使用 JDK 动态代理,否则使用 CGLIB 来生成代理类. 1.JDK 动态代理 那么接 ...
- JavaOOP-集合框架
1.Java集合框架包含的内容 Java集合框架为我们提供了一套性能优良,使用方便的接口和类,它们都位于在java.util包中. Collection 接口存储一组不唯一,无序的对象. List 接 ...
- HTTP缓存带来的“bug”--HTTP 协议 Cache-Control
问题描述 先说背景.网站是用PHP开发的,未用任何框架,代码结构也非常简单.运行于阿里云服务器,并采用其CDN来做分发.根据业务需求,有的页面会判断用户浏览器类型,依此来选择PC或者手机端内容. 在一 ...