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系统的时候但是又不想安装双系统,那么这个时候我们就可以试试这款虚拟软 件,如果你的电脑配置(主要是内存)够好的 ...
- 进入TP-Link路由器之后利用快捷键F12查看星号路由密码的方法
今天又破解了几个路由器,这两张图片是大多数路由器如TP-LINK路由器查看拨号圆点密码的方法.
- Canvas 画布组件(官网翻译)
Canvas画布 The Canvas is the area that all UI elements should be inside. The Canvas is a Game Object w ...
- canvas 绘制图形
canvas 绘制图形: 注意: canvas 的宽高设置在行内,否则会使画布(canvas)产生扭曲,绘图变形: <!DOCTYPE html> <html lang=" ...
- css多浏览常见问题
关于CSS对各个浏览器兼容已经是老生常谈的问题了, 网络上的教程遍地都是.以下内容没有太多新颖, 纯属个人总结, 希望能对初学者有一定的帮助. 一.CSS HACK 以下两种方法几乎能解决现今所有HA ...
- 黄金K线理论简述
黄金K线理论简述 [Ⅰ]. 隐藏在K线背后的多空搏杀 黄金K线的多空搏杀理论,说到底,其核心就是研判K线时,必须从多空搏杀的角度去认知,否则仅仅从表面到表面,是无法掌握K线精髓的.具体来说,多方和空方 ...
- 自然语言处理中的自注意力机制(Self-attention Mechanism)
自然语言处理中的自注意力机制(Self-attention Mechanism) 近年来,注意力(Attention)机制被广泛应用到基于深度学习的自然语言处理(NLP)各个任务中,之前我对早期注意力 ...
- V-bind详细使用
v-bind 主要用于属性绑定,Vue官方提供了一个简写方式 :bind,例如: <!-- 完整语法 --> <a v-bind:href="url">& ...
- python web开发-flask连接sqlite数据库
在之前的文章中我们介绍了如何在centOS中安装sqlite数据库. Sqlite安装完成后,本节就用flask来连接和操作sqlite数据库. 1. 数据准备 先在sqlite3中创建一 ...
- c++ --> 父类与子类间的继承关系
父类与子类间的继承关系 一.父类与子类 父类与子类的相互转换 1.派生类的对象可以赋给基类,反之不行 2.基类的指针可以指向派生类,反之不行 3.基类的引用可以初始化为派生类的对象,反之不行 4.派生 ...