鱼C论坛

 找回密码
 立即注册
查看: 2125|回复: 2

[已解决]爬虫中关于代理ip的问题。。。

[复制链接]
发表于 2020-11-15 18:26:11 | 显示全部楼层 |阅读模式

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

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

x

各位好,能帮忙试试这段代码吗?
内容:爬取 www.goubanjia.com 上的免费代理ip,并作 尝试 连接来观察其的有效性。
但 我连续爬取了数千条,一个可以用的都没发现。
是我电脑问题,还是,这网站上就没有可用的ip?

  1. from multiprocessing.pool import ThreadPool
  2. import requests
  3. from lxml import etree
  4. import time



  5. def open_url(url):
  6.     headers = {
  7.             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  8.     }
  9.     re = requests.get(url=url, headers=headers)
  10.     re.raise_for_status()
  11.     re.encoding = 'utf-8'
  12.     return re.text
  13.    
  14. def open_url_http(proxy):
  15.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',}
  16.     try:
  17.         res = requests.get(url=url_http, headers=headers, proxy=proxy)
  18.         assert res.status_code == 200
  19.         print('http://'+proxy, end='\t')
  20.         con = res.json()['origin']
  21.         if con == proxy:
  22.             print('http://'+proxy+' 可以使用')
  23.         else:
  24.             print('error')
  25.     except Exception as e:
  26.             pass

  27. def open_url_https(proxy):
  28.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',}
  29.     try:
  30.         res = requests.get(url=url_https, headers=headers, proxy=proxy)
  31.         assert res.status_code == 200
  32.         print('https://'+proxy, end='\t')
  33.         tree = etree.HTML(res.text)
  34.         con = tree.xpath('//body/p//text()')
  35.         if con[1] == proxy:
  36.             print('https://'+proxy+' 可以使用')
  37.         else:
  38.             print('error')
  39.     except Exception as e:
  40.             pass

  41. def get_proxies():
  42.     proxies_https = []
  43.     proxies_http = []
  44.     url = 'http://www.goubanjia.com/'
  45.     re_text = open_url(url=url)

  46.     tree = etree.HTML(re_text)
  47.     info_list = tree.xpath('//tbody/tr')
  48.     for each in info_list:
  49.         info_dict = {}

  50.         each_address_list = []
  51.         info_dict['address'] = each.xpath('./td[1]//*')
  52.         for each_address in info_dict['address']:
  53.             if each_address.xpath('./@style') == ["display: none;"] or each_address.xpath('./@style') ==["display:none;"]:
  54.                 pass
  55.             else:
  56.                 if len(each_address.xpath('./text()')) != 0:
  57.                     each_address_list.append(each_address.xpath('./text()')[0])      
  58.         info_dict['address'] = ''.join(each_address_list[:-1])+':'+each_address_list[-1]

  59.         info_dict['anonymity'] = each.xpath('./td[2]//text()')[0]
  60.         info_dict['protocal'] = each.xpath('./td[3]//text()')[0]
  61.         if info_dict['protocal'] == 'http':
  62.             if info_dict['address'] not in proxies_http:
  63.                 proxies_http.append(str(info_dict['address']))
  64.         elif info_dict['protocal'] == 'https':
  65.             if info_dict['address'] not in proxies_https:
  66.                 proxies_https.append(str(info_dict['address']))

  67.     # print(proxies_http)
  68.     # print(proxies_https)
  69.     return proxies_http, proxies_https
  70.    


  71. if __name__ == '__main__':
  72.    
  73.     url_https = 'https://202020.ip138.com/'
  74.     url_http = 'http://httpbin.org/ip'
  75.    
  76.    
  77.     def main():
  78.         proxies_http, proxies_https = get_proxies()
  79.         pool = ThreadPool(5)
  80.         print('main start')
  81.         for each_proxy in proxies_http:
  82.             proxy = {"http":f"{each_proxy}"}
  83.             pool.apply_async(open_url_http, (proxy,))
  84.         for each_proxy in proxies_https:
  85.             proxy = {"https":f"{each_proxy}"}
  86.             pool.apply_async(open_url_https, (proxy,))
  87.         pool.close()
  88.         pool.join()
  89.         print('main over')

  90.     for i in range(100):
  91.         main()
  92.         time.sleep(0.1)
  93.    
复制代码


最佳答案
2020-11-15 18:52:45
这个网站已经标注不保证可用了,你还是找客服要免费试用的吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-15 18:52:45 | 显示全部楼层    本楼为最佳答案   
这个网站已经标注不保证可用了,你还是找客服要免费试用的吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-15 22:26:13 | 显示全部楼层
suchocolate 发表于 2020-11-15 18:52
这个网站已经标注不保证可用了,你还是找客服要免费试用的吧。

好的,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 18:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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