爬取https://www.kuaidaili.com/free/inha/的代理ip,保存为IP.json文件
时间: 2023-11-11 09:05:18 浏览: 35
好的,我可以使用Python的requests和BeautifulSoup库来实现这个任务。以下是实现代码:
``` python
import requests
from bs4 import BeautifulSoup
import json
url = 'https://www.kuaidaili.com/free/inha/'
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
ips = soup.find_all('td', attrs={'data-title': 'IP'})
ports = soup.find_all('td', attrs={'data-title': 'PORT'})
proxies = []
for i in range(0, len(ips)):
proxy = ips[i].text + ':' + ports[i].text
proxies.append(proxy)
with open('IP.json', 'w') as f:
json.dump(proxies, f)
print('保存成功!')
```
这个代码会向指定网址发送请求,然后通过BeautifulSoup库解析HTML页面,找到其中的IP和PORT信息,最后将代理IP保存到IP.json文件中。