关于反反爬虫技术:对限制连续请求时间的处理

news/2024/7/19 9:25:22 标签: python, 爬虫, 反爬虫, 网络爬虫

一般的反爬措施是在多次请求之间增加随机的间隔时间,即设置一定的延时。但如果请求后存在缓存,就可以省略设置延迟,这样一定程度地缩短了爬虫程序的耗时。

下面利用requests_cache实现模拟浏览器缓存行为来访问网站,具体逻辑如下:存在缓存,就直接走,不存在缓存,就停一下再走

示例代码

用勾子函数根据缓存行为设置访问时间

python">import requests_cache
import time
requests_cache.install_cache()  #默认按照浏览器的缓存进行
requests_cache.clear()
def make_throttle_hook(timeout=0.1):
    def hook(response, *args, **kwargs):
        print(response.text)
          # 判断没有缓存时就添加延时
        if not getattr(response, 'from_cache', False):
                print(f'Wait {timeout} s!')
                time.sleep(timeout)
        else:
               print(f'exists cache: {response.from_cache}')
        return response
    return hook
if __name__ == '__main__':
    requests_cache.install_cache()
    requests_cache.clear()
    session = requests_cache.CachedSession() # 创建缓存会话
    session.hooks = {'response': make_throttle_hook(2)} # 配置钩子函数
    print('first requests'.center(50,'*'))
    session.get('http://httpbin.org/get')
    print('second requests'.center(50,'*'))
    session.get('http://httpbin.org/get')

有关requests_cache的更多用法,参考下面requests_cache说明

爬虫相关库

1. 爬虫常用的测试网站:httpbin.org

httpbin.org 这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。它用 Python + Flask 编写,是一个开源项目。

官方网站:http://httpbin.org/ 

开源地址:https://github.com/Runscope/httpbin

2. requests-cache

requests-cache,是 requests 库的一个扩展包,利用它可以非常方便地实现请求的缓存,直接得到对应的爬取结果。

GitHub:https://github.com/reclosedev/requests-cache 

PyPi:https://pypi.org/project/requests-cache/ 

官方文档:https://requests-cache.readthedocs.io/en/stable/index.html

作用和使用场景

1.在爬取过程中,它可以根据浏览器的缓存机制来选择缓存内容。从请求行为上看与浏览器更加相似,起到反反爬的效果。

2.另外,还可以自定义缓存机制,在爬虫项目中,优化性能。

requests-cache库只能对requests的请求实现缓存功能,而且requests要以session方式进行请求。单独的requests.get、requests.post 不能被缓存。 

requests使用方法

安装: 

python">pip install requests-cache

与普通的代码比较

在爬取一个域名下的多个url时,使用requests.session.get或requests.session.post会比单纯的requests.get、requests.post更高效。因为它只建立了一个会话,并在上面做多次请求。同时还支持登录信息cookie等的传递。

下面比较一下缓存代码的写法 没有缓存的代码:

普通的requests session爬取

python">import requests
import time
python">start = time.time()
session = requests.Session()
for i in range(10):
    session.get('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time', end - start)

该代码是访问了httpbin.org网站,该网站会解析delay/1,在1秒后返回。

有缓存的代码:

带缓存的requests session爬取

python">import requests_cache #pip install requests_cache
import time
python">start = time.time()
session = requests_cache.CachedSession('demo_cache')
python">for i in range(10):
    session.get('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time', end - start)

为原有代码微创式添加缓存功能

只需要添加一句requests_cache.install_cache('demo_cache')即可。

微创式添加缓存功能

python">import requests_cache #pip install requests_cache
requests_cache.install_cache('demo_cache')#demo_cache.sqlite 做缓存

import requests
import time

start = time.time()
session = requests.Session()
for i in range(10):
    session.get('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time', end - start)

缓存的清空和识别

如果需要清空缓存,可以调用:requests_cache.clear() # 清空缓存代码

通过res.from_cache可以判断该值是否是缓存值:

python">import requests_cache
import requests
requests_cache.install_cache() # 设置缓存
requests_cache.clear() # 清空缓存
url = 'http://httpbin.org/get'
res = requests.get(url)
print(f'cache exists: {res.from_cache}')
# cache exists: False # 不存在缓存

res = requests.get(url)
print(f'exists cache: {res.from_cache}')
# exists cache: True # 存在缓存

自定义设置缓存的形式

requests_cache.install_cache默认的方式是与浏览器的缓存行为一致的。如果要自定义可以先了解该函数的参数:

requests_cache.install_cache定义

python">requests_cache.install_cache(
    cache_name='cache',
    backend=None,
    expire_after=None,
    allowable_codes=(200,),
    allowable_methods=('GET',),
    filter_fn=<function <lambda> at 0x11c927f80>,
    session_factory=<class 'requests_cache.core.CachedSession'>,
    **backend_options,
)

该参数说明如下: - cache_name:缓存文件名称。

  • backend:设置缓存的存储机制,默认使用sqlite进行存储。

    支持四种不同的存储机制,分别为memory、sqlite、mongoDB、redis。在设置存储机制为mongoDB、redis时需要提前安装对应的模块。pip install pymongo; pip install redies。 

  • memory:以字典的形式将缓存存储在内存当中,程序运行完以后缓存将被销毁 

  • sqlite:将缓存存储在sqlite数据库中 

  • mongoDB:将缓存存储在mongoDB数据库中 

  • redis:将缓存存储在redis中 

  • expire_after:设置缓存的有效时间,默认永久有效。 

  • allowable_codes:设置状态码。 

  • allowable_methods:设置请求方式,默认get,表示只有get请求才可以生成缓存。 

  • session_factory:设置缓存执行的对象,需要实现CachedSession类。 

  • **backend_options:如果缓存的存储方式为sqlit、mongo、redis数据库,该参数表示设置数据库的连接方式。

自定义设置缓存的例子1:设置缓存文件类型

设置缓存文件类型的代码如下:

python">#设置缓存:任选其一
requests_cache.install_cache('demo_cache')#demo_cache.sqlite 做缓存
#demo_cache文件夹做缓存,删除及表示清空缓存
requests_cache.install_cache('demo_cache', backend='filesystem')
#缓存文件夹便会使用系统的临时目录,而不会在代码区创建缓存文件夹。
requests_cache.install_cache('demo_cache', backend='filesystem', use_temp=True)
#缓存文件夹便会使用系统的专用缓存文件夹,而不会在代码区创建缓存文件夹
requests_cache.install_cache('demo_cache', backend='filesystem', use_cache_dir=True)
python">#Redis  ,需要安装redis-py  pip install redies
backend = requests_cache.RedisCache(host='localhost', port=6379)
requests_cache.install_cache('demo_cache', backend=backend)

其他不同格式:

  • MongoDB 安装pymongo pip install pymongo;

    调用requests_cache.MongoCache 保存为’mongodb’

  • gridfs 安装pymongo

    调用requests_cache.GridFSCache 保存为’gridfs’

  • DynamoDB boto3 调用requests_cache.DynamoDbCache 保存为’dynamodb’ 

  • Memory 以字典的形式将缓存存储在内存当中,程序运行完以后缓存将被销毁 调用requests_cache.BaseCache 保存为’memory’

自定义设置缓存的例子2:设置缓存保存内容

具体例子代码如下:

只缓存200返回值的请求

python">import time
import requests
import requests_cache

#只缓存post
requests_cache.install_cache('demo_cache2', allowable_methods=['POST'])

#只缓存200返回值的请求
requests_cache.install_cache('demo_cache2', allowable_codes=(200,))

设置缓存的过期时间:

python">#site1.com 的内容就会缓存 30 秒,site2.com/static 的内容就永远不会过期
urls_expire_after = {'*.site1.com': 30, 'site2.com/static': -1}
requests_cache.install_cache(
    'demo_cache2', urls_expire_after=urls_expire_after)

在响应头中,浏览器会根据cache_control参数来确定是否保存缓存,在设置requests_cache缓存时,可以对cache_control参数设置,使其保存浏览器不需要保存的内容。

python"># 保存头中,cache_control设为不保存的请求
requests_cache.install_cache('demo_cache3', cache_control=True)

start = time.time()
session = requests.Session()
for i in range(10):
    session.get('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time for get', end - start)
start = time.time()

for i in range(10):
    session.post('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time for post', end - start)

在 Request Headers 里面加上了 Cache-Control 为 no-store,这样的话,即使我们声明了缓存那也不会生效。

python">session.get('http://httpbin.org/delay/1',
                headers={
                    'Cache-Control': 'no-store'
                })

http://www.niftyadmin.cn/n/1401582.html

相关文章

python超链接按钮_创建执行python脚本的超链接(或按钮),然后在脚本完成时重定向...

解决这个问题的一个简单方法是使用一个简单的web框架(比如Flask)来构建系统的web部件。在魔术链接的请求处理程序中&#xff0c;需要生成脚本并跟踪它。查看脚本是否完成并将其转发给用户的一个简单方法是定期发送ajax请求以检查是否完成。例如&#xff0c;Flask网站可能看起来…

这5种炫酷的动态图,都是用Python实现的

数据可以帮助我们描述这个世界、阐释自己的想法和展示自己的成果&#xff0c;但如果只有单调乏味的文本和数字&#xff0c;我们却往往能难抓住观众的眼球。而很多时候&#xff0c;一张漂亮的可视化图表就足以胜过千言万语。 本文将介绍 5 种基于 Plotly 的可视化方法&#xff0…

Python练手小例子,闯关60题

01 数字 1 求绝对值 绝对值或复数的模 In [1]: abs(-6) Out[1]: 62 进制转化 十进制转换为二进制&#xff1a; In [2]: bin(10) Out[2]: 0b1010十进制转换为八进制&#xff1a; In [3]: oct(9) Out[3]: 0o11十进制转换为十六进制&#xff1a; In [4]: hex(15) Out[4]: 0x…

python爬去微博十大流行语_用python重新定义【2019十大网络流行语】

前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。作者&#xff1a;朱小五PS&#xff1a;如有需要Python学习资料的小伙伴可以加点击下方链接自行获取12月1日&#xff0c;《咬文嚼字》编辑部以一首“顺…

python123第一周数值运算_python123第一周作业

1.使用turtle库绘制一个红色五角星图形import turtleturtle.pensize(4)turtle.pencolor("black")turtle.fillcolor("red")turtle.begin_fill()for _ in range(5):turtle.forward(200)turtle.right(144)turtle.end_fill()2.使用turtle库绘制一个六角形impor…

10 个 Python 项目简单又超有趣

Python可谓是现在很多人正在学或者想学的一个脚本语言了&#xff0c;提到学习自然就少不了拿项目练手&#xff0c;可是一般的项目根本提不起兴趣嘛&#xff0c;这10个项目可是非常有趣的&#xff0c;不信你看看。 【Python 图片转字符画】 用 50 行 Python 代码完成图片转字符…

用python写一个简单的贪吃蛇游戏(附代码)

不知道有多少同学跟我一样&#xff0c;最初接触编程的动机就是为了自己做个游戏玩&#xff1f; 今天要给大家分享的是一个 pygame 写的“贪吃蛇”小游戏&#xff1a; “贪吃蛇”这个小游戏在编程学习中的常客&#xff0c;因为&#xff1a; 简单&#xff0c;最基本的游戏元素只…

sql累计求和时间太长_SQL高级功能-窗口函数

一.定义&#xff1a;窗口函数也就叫OLAP函数&#xff0c;可以对数据库数据进行实时分析处理二.基本语法‹窗口函数› over (partition by ‹用于分组的列名› order by ‹用于排序的列名›)三.分类1.专用窗口函数如rank, dense_rank, row_number等专用窗口函数rank, dense_rank…