鱼C论坛

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

[作品展示] Python爬虫,抓取代理地址,可配合其他工具使用

[复制链接]
发表于 2018-4-24 16:18:59 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 fl93 于 2018-4-24 16:57 编辑

RT


做完了python入门的所有作业

老甲鱼视频里说做一个自动抓代理地址的爬虫

经过尝试,代码在最后

主要作为配合其他工具代理使用

纯新手,请老司机们刀下留情

有任何问题和建议请留言,期待你的回复

  1. import urllib.request
  2. import urllib.parse
  3. import chardet
  4. import gzip
  5. import json
  6. import re
  7. from bs4 import BeautifulSoup



  8. def get_proxylist(proxy_info=None):
  9.     '''
  10.     get_proxylist() is a func to get proxy ip addresses from http://www.gatherproxy.com/
  11.     it will return proxy informations with a list
  12.     each element in this list is a dictionary


  13.     proxy_info is the proxy information to hiding your real ip address
  14.     if you can not connect to http://www.gatherproxy.com or you get block by this server
  15.     parameter format is like blow:
  16.     "ip:port" --> "192.168.1.1:8080"

  17.    
  18.     you can access by folowing keywords

  19.     important keyword:
  20.     "PROXY_COUNTRY"  --> Proxy location
  21.     "PROXY_IP"       --> Proxy service IP
  22.     "PROXY_PORT"     --> Need to covert from Hex to Decimal(now is automatically coverted)
  23.     "PROXY_STATUS"   --> OK is the default status
  24.     "PROXY_UPTIMELD" --> Max capacity and how many people are using this proxy
  25.    
  26.     '''
  27.    
  28.     url = 'http://www.gatherproxy.com/'

  29.     header = {
  30.     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.281 Safari/537.36'
  31.     }
  32.     try:
  33.    
  34.         if proxy_info:
  35.             proxy_support = urllib.request.ProxyHandler({'http':proxy_info})
  36.             req = urllib.request.Request(url=url, headers=header)
  37.             opener = urllib.request.build_opener(proxy_support)
  38.             
  39.         else:
  40.             req = urllib.request.Request(url=url, headers=header)
  41.             opener = urllib.request.build_opener()

  42.         response = opener.open(req)
  43.    
  44.         html = response.read()
  45.         
  46.         encode = chardet.detect(html)['encoding']
  47.         #print('当前编码为:%s\n'%encode)
  48.         if encode == 'GB2312':
  49.             encode = 'GBK'
  50.             
  51.         #if not encode:
  52.         #    html = gzip.decompress(html)
  53.         #    encode = chardet.detect(html)['encoding']
  54.         #    print('当前编码为:%s\n'%encode)

  55.             
  56.         html = html.decode(encode)
  57.         soup = BeautifulSoup(html, 'html.parser')

  58.         proxy_list = list()
  59.         
  60.         proxy_js = soup.find_all(type="text/javascript")
  61.         for each_js in proxy_js:
  62.             tmp = re.search(r'gp.insertPrx\(({.+\})\)', each_js.text)
  63.             if tmp:
  64.                 tmp = json.loads(tmp.group(1))
  65.                 port_hex = tmp.get("PROXY_PORT")
  66.                 tmp["PROXY_PORT"] = int(port_hex, 16)
  67.                 proxy_list.append(tmp)
  68.         
  69.         return proxy_list

  70.     except ConnectionResetError as e:
  71.         print('Your ip address has been blocked by this server! please try again with proxy_info parameter filled\n')
  72.         print('Error Information is: %s' %e)
  73.    
  74.     except urllib.error.URLError as e:
  75.         print('Failed to connect to the proxy ip! please try again with new proxy information\n')
  76.         print('Error Information is: %s' %e)
复制代码

评分

参与人数 1荣誉 +10 收起 理由
alltolove + 10 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 04:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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