3 爬虫入门指南:Python网络请求及常见反爬虫策略应对方法( 二 )


import timeimport randomimport requests# 引入随机延迟headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}for page in range(1, 11):response = requests.get(f'http://example.com?page={page}', headers=headers)# 处理响应数据# ...delay = random.uniform(0.5, 1.5)# 随机延迟,范围为0.5到1.5秒time.sleep(delay)
未完待续…