python怎么从网上爬到可用代理ip我需要免费的代理ip
要从网上爬取可用的免费代理IP,你可以使用Python中的第三方库,比如requests和BeautifulSoup。以下是一个示例代码:
import requests
from bs4 import BeautifulSoup
def get_proxy_ips():
url = 'https://www.free-proxy-list.net/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', id='proxylisttable')
ips = []
for row in table.find_all('tr')[1:]:
columns = row.find_all('td')
ip = columns[0].text
port = columns[1].text
protocol = columns[6].text.lower()
if protocol == 'yes':
ips.append(f'{protocol}://{ip}:{port}')
return ips
# 测试爬取的代理IP是否可用
def test_proxy_ip(proxy_ip):
url = 'https://www.google.com/'
try:
response = requests.get(url, proxies={'https': proxy_ip}, timeout=5)
if response.status_code == 200:
print(f'{proxy_ip} is working')
except:
print(f'{proxy_ip} is not working')
# 获取代理IP列表
proxy_ips = get_proxy_ips()
# 遍历测试代理IP是否可用
for proxy_ip in proxy_ips:
test_proxy_ip(proxy_ip)
这个示例代码使用了https://www.free-proxy-list.net/网站来获取免费的代理IP列表,并检查是否可用。请注意,免费的代理IP可能不稳定,并且不能保证始终可用
原文地址: https://www.cveoy.top/t/topic/hLNg 著作权归作者所有。请勿转载和采集!