鱼C论坛

 找回密码
 立即注册
查看: 1940|回复: 1

将普通程序加入代理池功能,如有指导~感谢

[复制链接]
发表于 2021-10-13 11:32:03 | 显示全部楼层 |阅读模式

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

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

x
描述:想要一个普通的豆瓣抓取代码(抓什么的都行)然后把这个代理功能加进入。
问题:现在就是大概可以看懂代理池这个代码,想把这个功能用到真正抓取的功能里边
老师上课的代码:
  1. # https://blog.csdn.net/qq_40418612/article/details/82595140
  2. import random, requests, os
  3. from selenium import webdriver
  4. from lxml import etree
  5. import time
  6. def req(url):
  7.     # 设置多个 user-agent
  8.     User_Agent = random.choice([
  9.         "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
  10.         "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
  11.         "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
  12.         "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
  13.         "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
  14.         "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
  15.         "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
  16.         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
  17.         "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
  18.         "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
  19.         "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
  20.         "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
  21.         "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
  22.         "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
  23.         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
  24.         "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
  25.         'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
  26.     ])
  27.     # 从https://www.zdaye.com爬取代理,并存储到http_list.txt和https_list.txt的文件中
  28.     l = os.listdir(os.getcwd())
  29.     if "http_list.txt" not in l:
  30.         proxy_url = 'https://www.zdaye.com/dayProxy/ip/324679.html'
  31.         driver = webdriver.Chrome()
  32.         driver.get(proxy_url)
  33.         time.sleep(2)
  34.         t = driver.page_source
  35.         # driver.quit()

  36.         html = etree.HTML(t)
  37.         print(t)
  38.         http_list = []
  39.         https_list = []
  40.         wholeProxys = html.xpath("//*[@class='cont']/text()")
  41.         # print(len(wholeProxys))
  42.         for each in wholeProxys:
  43.             wholeProxy=each[:each.find('#')]  #通过索引访问字符串;下同。
  44.             ip=wholeProxy[:wholeProxy.find(':')]
  45.             port=wholeProxy[wholeProxy.find(':')+1:wholeProxy.find('@')]
  46.             protocol=wholeProxy[wholeProxy.find('@')+1:]
  47.             # print(wholeProxy)
  48.             print(ip,'--',port,'--',protocol)

  49.             if protocol == "HTTP":
  50.                 http_list.append(protocol + r"://" + ip + ":" + port)
  51.             if protocol == "HTTPS":
  52.                 https_list.append(protocol + r"://" + ip + ":" + port)
  53.         with open("http_list.txt", "w") as p:
  54.             p.write(str(http_list))
  55.         with open("https_list.txt", "w") as p:
  56.             p.write(str(https_list))
  57.     else:
  58.         with open("http_list.txt") as f:
  59.             http_list = eval(f.read())
  60.         with open("https_list.txt") as f:
  61.             https_list = eval(f.read())

  62.     try:
  63.         proxies = {"http": random.choice(http_list), "https": random.choice(https_list)}
  64.         headers = {"User-Agent": User_Agent, "cookie": revert_cookie(cookie)}
  65.         r = requests.get(url, proxies=proxies, headers=headers)
  66.         print(r.status_code)
  67.         return r.text
  68.     except:
  69.         print("当前所使用的代理已失效,更换代理重试中...")
  70.         while True:
  71.             for n in range(100):
  72.                 # proxies = {"http": random.choice(list(http_list)), "https": random.choice(list(https_list))}
  73.                 proxies = {"http": random.choice(list(http_list))}  #暂不处理https
  74.                 headers = {"User-Agent": User_Agent, }
  75.                 r = requests.get(url, proxies=proxies, headers=headers)
  76.                 if r.status_code == 200:
  77.                     print("更换代理成功,已获取到网页源码;当前所用代理为:",proxies)
  78.                     print(r.text)
  79.                     return r
  80.                 else:
  81.                     continue
  82.             print('在代理池中更换了n个代理,但还是无法连接服务器...')


  83. if __name__ == '__main__':
  84.     url = "http://httpbin.org/ip"
  85.     print(req(url))
复制代码


没找到悬赏在哪...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-13 20:51:44 From FishC Mobile | 显示全部楼层
免费代理就不要玩了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 17:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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