Python中使用代理IP进行网站监测的实现方法
- 首先需要安装requests库,使用以下命令安装:
pip install requests
- 然后引入requests库和随机模块:
python
import requests
import random
- 定义代理IP池,可以从免费代理网站获取代理IP地址:
python
proxy_list = [
'http://ip1:port1',
'http://ip2:port2',
'http://ip3:port3',
...
]
- 定义请求头,一般需要添加User-Agent和Accept-Language:
python
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',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
- 编写请求函数,并指定代理IP:
```python
def request(url):
# 随机选取一个代理IP
proxy = random.choice(proxy_list)
proxies = {'http': proxy, 'https': proxy}
# 发送请求 response = requests.get(url, headers=headers, proxies=proxies, timeout=10) # 返回响应内容 return response.content
```
- 使用请求函数获取网站内容:
python
url = 'http://pidancode.com'
content = request(url)
print(content.decode('utf-8'))
- 完整代码演示:
```python
import requests
import random
proxy_list = [
'http://ip1:port1',
'http://ip2:port2',
'http://ip3:port3',
...
]
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',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
def request(url):
# 随机选取一个代理IP
proxy = random.choice(proxy_list)
proxies = {'http': proxy, 'https': proxy}
# 发送请求 response = requests.get(url, headers=headers, proxies=proxies, timeout=10) # 返回响应内容 return response.content
url = 'http://pidancode.com'
content = request(url)
print(content.decode('utf-8'))
```
相关文章