鱼C论坛

 找回密码
 立即注册
查看: 2147|回复: 0

[技术交流] 快速获取ip代理并检测获取代理的有效性

[复制链接]
发表于 2020-5-9 23:21:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding = utf-8 -*-
  2. # @清凉
  3. # @快速获取ip代理并检测获取代理的有效性.py

  4. import requests
  5. import pprint
  6. import parsel
  7. import time


  8. headers = {
  9.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36 Edg/81.0.416.68',
  10.     'Cookie': 'channelid=0; sid=1588905849664960; Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1588905972; _ga=GA1.2.620440764.1588905972; _gid=GA1.2.1803663967.1588905972; MEIQIA_TRACK_ID=1bbg0JvBZbe016Kxzb1t3ZCj7tC; MEIQIA_VISIT_ID=1bbg0Lh0pypxP2MrSeKFT8zQg8V; _gat=1; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=1588906165'}


  11. iplist = []
  12. # num=int(input("请输入你想要获取的代理ip数量,建议范围15以上,1000以内"//int(15)))
  13. num = int(input("请输入你想要获取的代理ip数量,建议范围15以上,1000以内:"))
  14. num = num // 15 + 2
  15. timewait = int(input("请输入你想要的速度,1最快,10最慢"))
  16. for i in range(1, num):
  17.     url = 'https://www.kuaidaili.com/free/inha/{}/'.format(str(i))
  18.     response = requests.get(url, headers=headers, timeout=5)
  19.     data = response.content.decode("utf-8")  # 这个地方出过错
  20.     html = parsel.Selector(data)
  21.     temp = html.xpath(
  22.         "//table[@class='table table-bordered table-striped']/tbody/tr")

  23.     for item in temp:
  24.         ipdata = {}
  25.         ip_class = item.xpath('./td[4]/text()').extract_first()
  26.         ip_num = item.xpath('./td[1]/text()').extract_first()
  27.         ip_port = item.xpath('./td[2]/text()').extract_first()
  28.         ip_add = item.xpath('./td[5]/text()').extract_first()
  29.         ipdata[ip_class] = ip_num + ':' + ip_port
  30.         iplist.append(ipdata)

  31.     time.sleep(timewait)
  32.     print("第{}页已经获取完毕".format(i))

  33.     # print(ip_class,ip_num,ip_port,ip_add)
  34. print(len(iplist))
  35. print("*" * 100)
  36. # print(html)


  37. def checkbaidu(iplist):
  38.     '''快速检测代理有效性'''

  39.     global can_use
  40.     can_use = []
  41.     for ip in iplist:
  42.         try:
  43.             temp = requests.get(
  44.                 'https://www.baidu.com',
  45.                 headers=headers,
  46.                 timeout=2,
  47.                 proxies=ip)
  48.             if temp.status_code == 200:
  49.                 can_use.append(ip)
  50.         except BaseException:
  51.             print(ip, "不能使用或超时了")
  52.     print("一共有{}个可以有效使用".format(len(can_use)))


  53. print("##" * 20)
  54. checkbaidu(iplist)
  55. print("##" * 20)
  56. print("他们分别是:")
  57. print(can_use)
复制代码


效果如下,希望指点


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表