Python 反爬虫代码可以通过以下几种方法实现:
使用 `robots.txt` 文件
创建一个 `robots.txt` 文件,并添加 `Disallow` 规则来阻止爬虫访问特定页面。
```
User-agent: *
Disallow: /private/
Disallow: /admin/
设置 HTTP 标头
在请求中添加 `User-Agent` 和 `Retry-After` 等 HTTP 标头,模拟浏览器行为。
```python
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Retry-After': '5'
}
url = 'https://www.example.com'
response = requests.get(url, headers=headers)
IP代理
使用代理服务器来隐藏爬虫的真实 IP 地址。
```python
import requests
proxies = {
'http': 'http://127.0.0.1:8888',
'https': 'https://127.0.0.1:8888'
}
url = 'https://www.example.com'
response = requests.get(url, proxies=proxies)
随机延时
在请求之间添加随机延时,模拟人类浏览网页的行为。
```python
import requests
import time
import random
url = 'https://www.example.com'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
time.sleep(random.uniform(1, 5)) 随机延时1到5秒
response = requests.get(url, headers=headers)
禁用 HTTP 1.x 请求
使用支持 HTTP/2.0 的库,如 `httpx`,来禁用 HTTP 1.x 请求。
```python
import httpx
client = httpx.Client(http2=True)
response = client.get('https://www.example.com')
User-Agent 伪装
在请求头中设置 `User-Agent`,模拟不同的浏览器。
```python
from urllib.request import urlopen
req = urllib.request.Request('https://www.example.com')
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
response = urllib.request.urlopen(req)
使用第三方库获取 User-Agent
使用 `faker` 库来随机生成 User-Agent。
```python
pip install faker
from faker import Faker
fake = Faker()
headers = {
'User-Agent': fake.user_agent()
}
请根据目标网站的具体反爬虫策略选择合适的方法,并注意遵守网站的使用条款和法律法规。