当前位置:网站首页>爬虫——爬虫初识、requests模块
爬虫——爬虫初识、requests模块
2022-08-02 07:32:00 【山上有个车】
系列文章目录
第二章 代理搭建、爬取视频网站、爬取新闻、BeautifulSoup4介绍、bs4 遍历文档树、bs4搜索文档树、bs4使用选择器
文章目录
前言
爬虫是非正规,且违法的,所以本博客主旨在于交流学习
一、爬虫初识
爬虫实质上就是模拟浏览器向服务器发送请求获取数据。爬虫做的好不好主要在于对模拟浏览器的请求是否足够真实,只要骗过服务器,之后再对获取到的数据进行清洗,再保存下来。
爬虫有爬虫协议:
只要遵守爬虫协议,爬虫就不会违法。但我们爬虫一般都只会爬取一些不在服务器robots.txt规定中的数据。
二、requests模块
1.requests模块介绍
urllib内置模块、requests模块的api,可以发送http请求,但是api使用复杂,这个时候可以使用requests模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3),该模块不仅仅用于爬虫,服务器与其他服务器交流也能使用requests模块。
注意:requests库发送请求将网页内容下载下来以后,并不会执行js代码,这需要我们自己分析目标站点然后发起新的request请求
2.requests安装
pip3 install requests
3.携带get参数
import requests
# 方法一直接带在url中
res = requests.get('https://www.xxsy.net/search?&s_wd=将军,夫人')
# 方法二在params携带
params_dict = {
's_wd':'将军,夫人'}
res1 = requests.get('https://www.xxsy.net/search', params=params_dict)
4.携带请求头
常见请求头:
user-agent:客户端类型
Referer: https://www.lagou.com/gongsi/ 上一次访问的地址
import requests
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
res=requests.get('https://dig.chouti.com/',headers=header)
5.携带cookie
import requests
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
# 方式一:带在请求头中
res = requests.post('https://dig.chouti.com/link/vote',
headers=header,
data={
'linkId': '35811284'
})
print(res.text)
# 方式二:使用cookie参数:之前登录成功了,就有cookie,cookie是CookieJar的对象,直接传
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
res = requests.post('https://dig.chouti.com/link/vote',
headers=header,
# Dict or CookieJar
# cookies={},
data={
'linkId': '35811284'
})
print(res.text)
6.post请求
res = requests.post('http://www.aa7a.cn/user.php', data={
'username': '@qq.com',
'password': '',
'captcha': 'aaaa',
'remember': 1,
'ref': 'http://www.aa7a.cn/user.php?act=logout',
'act': ' act_login',
})
print(res.text)
print(res.cookies) # 登录成功的cookie,cookieJar对象是一个字典
# 登录成功
res1=requests.get('http://www.aa7a.cn/',cookies=res.cookies)
print('[email protected]' in res1.text)
7.post请求携带数据
post请求有三种携带数据的格式:
from-data、urlencoded(默认)、json
res=requests.post('xxx',json={
}) #使用关键字参数指定即可
8.session
requests.session的使用,整个过程中自动维护cookie
session=requests.session()
# 使用session发送请求
session.post('http://www.aa7a.cn/user.php', data={
'username': '[email protected]',
'password': 'bbc123',
'captcha': 'aaaa',
'remember': 1,
'ref': 'http://www.aa7a.cn/user.php?act=logout',
'act': ' act_login',
})
res1=session.get('http://www.aa7a.cn/')
print('[email protected]' in res1.text)
9 response属性
repsonse对象的属性和方法,把http的响应封装成了response
respone=requests.get('https://www.cnblogs.com/')
respone=requests.get('http://www.autohome.com/news')
print(respone.text) # 响应体的字符串
print(respone.content) # 响应体二进制数据
print(respone.status_code) #响应状态码
print(respone.headers)# 响应头
print(respone.cookies) #响应的cookie
print(respone.cookies.get_dict()) #cookie转成dict
print(respone.cookies.items()) # cookie拿出key和value
print(respone.url) # 请求的地址
print(respone.history) # 列表,有重定向,里面放了重定向之前的地址
print(respone.encoding) # 响应编码格式
# 后期下载图片,视频,需要使用它
respone.iter_content()
res=requests.get('https://video.pearvideo.com/mp4/adshort/20220427/cont-1760318-15870165_adpkg-ad_hd.mp4')
with open('sp3.mp4','wb') as f:
# f.write(res.content)
for line in res.iter_content(chunk_size=1024): # 按1024字节写
f.write(line)
10 编码问题
编码问题:大部分网站都是utf-8编码,老网站中文编码使用gbk,gb2312
# respone = requests.get('http://www.autohome.com/news')
# # respone.encoding='gbk'
# print(respone.text) # 默认使用utf-8可能会导致中文乱码
11 获取二进制数据
response.content
response.iter_content(chunk_size=1024)
11 解析json
res = requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword', data={
'cname': '',
'pid': '',
'keyword': '北京',
'pageIndex': 1,
'pageSize': 10,
})
import json
dic_res = json.loads(res.text)
print(type(dic_res))
print(dic_res['Table1'][0]['storeName'])
res = requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword', data={
'cname': '',
'pid': '',
'keyword': '北京',
'pageIndex': 1,
'pageSize': 10,
})
print(type(res.json()))
13高级用法之 Cert Verification(仅了解)
高级用法之证书(仅了解)
发送https请求,浏览器内置了证书,如果网站与的证书是第三方的就会提示整数过期,这个时候发送请求手动携带证书,才能获取到对应数据
14 代理
代理ip之后网站的频率限制就无法通过限制ip,黑名单(封ip)来限制我们发送大量请求获取数据
代理搭建在下一篇博客中
import requests
proxies = {
'http': '112.14.47.6:52024',
}
#180.164.66.7
respone=requests.get('https://www.cnblogs.com/',proxies=proxies)
print(respone.status_code)
15 超时设置
import requests
respone=requests.get('https://www.baidu.com',timeout=0.0001)
16 异常处理
from requests.exceptions import *
try:
r=requests.get('http://www.baidu.com',timeout=0.00001)
# except ReadTimeout: 读取超时
# print('===:')
# except ConnectionError: #网络不通
# print('-----')
# except Timeout: # 等待时间超时
# print('aaaaa')
except Exception: # 全异常捕获
print('x')
17 上传文件
import requests
files={
'file':open('a.jpg','rb')}
respone=requests.post('http://httpbin.org/post',files=files)
print(respone.status_code)
边栏推荐
- playwright 爬虫使用
- 论文理解:“Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution,Denoising, and “
- MySQL database design specification
- gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
- pnpm + workspace + changesets 构建你的 monorepo 工程
- [Unity3D] Beginner Encryption Skills (Anti-Cracking)
- MySQL报错1055解决办法:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
- Appium 滑动问题
- flutter 参数传一个范型数据
- MySQL-底层设置
猜你喜欢
停止精神内耗 每日分享
MySQL - Index Optimization and Query Optimization
MySQL事务(transaction) (有这篇就足够了..)
sql创建表格 如图 运行完提示invalid table name 是什么原因
典型的一次IO的两个阶段是什么?阻塞、非阻塞、同步、异步
MySQL-底层设置
FormData upload binary file, object, object array
PLSQL Developer安装和配置
MPLS和BGP的综合实验
MySQL-Execution Process + Cache + Storage Engine
随机推荐
机器学习笔记--数学库
MySQL - based
关于DDoS的几个误区
postgres groupby 合并字符串
flutter解决键盘和输入框不适配问题
RIP综合实验
MySQL之创建表的基本操作
Data Middle Office: Started in Ali, Prosperous in DaaS
Conditional constructor ~wapper
Mysql error 2003 solution Can 't connect to Mysql server on' localhost '(10061).
Hack The Box - File Transfers Module详细讲解中文教程
常用的云安全防护措施盘点
MySQL - locking mechanism
7.联合索引(最左前缀原则)
MySQL error 1055 solution: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
MySQL-底层设置
redis-高级篇
Kind of weird!Access the destination URL, the host can container but not
Thesis understanding: "Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution, Denoising, and "