python爬虫获取代理免费ip并检测

60次阅读
没有评论

以下IP获取来自[神龙HTTP]https://h.shenlongip.com/

注册之后每天可领免费的IP,有时效

import requests import json from bs4 import BeautifulSoup import time import random url = ‘http://httpbin.org/get’ url_baidu = ‘https://www.baidu.com/’ proxy_list = [ ‘124.112.237.98:4286’, ‘124.112.189.253:4265’, ‘183.164.77.44:4265’, ‘123.97.109.75:4258’, ‘27.25.97.97:4205’, ‘122.192.230.149:4278’, ‘111.79.173.45:5632’ ] proxy_ip = ‘http://’ + random.choice(proxy_list)#随机获取代理IP proxies = {‘http’:proxy_ip} print(proxies) try: res = requests.get(url,proxies=proxies,timeout=10)#timeout设置响应时间,防止响应时间过长 if res.status_code == 200: print(res.status_code) print(res.elapsed)#响应时间 0:00:06.076746 print(res.elapsed.total_seconds())#响应时间,单位/秒 6.076746 soup = BeautifulSoup(res.text,’lxml’) p = soup.select(‘p’)[0].text print(json.loads(p)[‘origin’])# 218.95.97.49 和proxies一致 else: print(‘IP无效’) except: print(‘IP无效’)

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-27发表,共计807字。
新手QQ群:570568346,欢迎进群讨论 Python51学习