论坛风格切换切换到宽版
  • 25阅读
  • 0回复

关于ip代理的三个模块 [复制链接]

上一主题 下一主题
离线北斗星
 

只看楼主 倒序阅读 使用道具 楼主   发表于: 2019-03-23
  1. #!/usr/bin/env python
  2. # coding:utf8
  3. # author:Z time:2018/8/16
  4. import random
  5. import pymysql
  6. import requests
  7. def get_ip():
  8.     """
  9.     获取ip代理池里的ip
  10.     :return:
  11.     """
  12.     connect = pymysql.Connect(
  13.         host='127.0.0.1',
  14.         port=3306,
  15.         user='root',
  16.         password='',
  17.         db='haha',
  18.         charset='utf8'
  19.     )
  20.     # connect = pymysql.Connect(
  21.     #             host='10.10.141.235',
  22.     #             port=3306,
  23.     #             user='gtcom',
  24.     #             passwd='admin@gt.com1',
  25.     #             db='big_data',
  26.     #             charset='utf8'
  27.     #         )
  28.     sql1 = """
  29.     select ip_address from ip_proxy
  30.     """
  31.     sql2 = """
  32.         select port from ip_proxy
  33.         """
  34.     sql3 = """
  35.             select type_ from ip_proxy
  36.             """
  37.     cursor = connect.cursor()
  38.     sql4 = """
  39.     select count(*) from ip_proxy
  40.     """
  41.     cursor.execute(sql4)
  42.     a = cursor.fetchall()  # ((0,),)
  43.     if a[0][0] == 0:  # 判断数据库是否为空
  44.         print('没有数据')
  45.         # 调用付费代理ip
  46.         html = requests.get(
  47.             'http://webapi.http.zhimacangku.com/getip?num=1&type=1&pro=&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1®ions=')
  48.         pay_ip_proxy = html.text.strip()
  49.         s_ip_proxy = pay_ip_proxy.split(':')
  50.         ip_addresses = s_ip_proxy[0]
  51.         ports = s_ip_proxy[1]
  52.         type_s = 'HTTP'
  53.         return ip_addresses,ports,type_s
  54.     else:
  55.         cursor.execute(sql1)
  56.         ip_addresses = cursor.fetchall()
  57.         # ip_address=random.choice(ip_addresses)[0]
  58.         cursor.execute(sql2)
  59.         ports = cursor.fetchall()
  60.         # port=random.choice(ports)[0]
  61.         cursor.execute(sql3)
  62.         type_s = cursor.fetchall()
  63.         # type_=random.choice(type_s)[0]
  64.         return ip_addresses,ports,type_s
  65. ip_addresses,ports,type_s = get_ip()
  66. def change_ip():
  67.     """
  68.     随机切换ip
  69.     :return:
  70.     """
  71.     ip_address = random.choice(ip_addresses)[0]
  72.     port = random.choice(ports)[0]
  73.     type_ = random.choice(type_s)[0]
  74.     proxies = {
  75.         type_: type_ + '://' + ip_address + ':' + port
  76.     }
  77.     return proxies,ip_address
  78. def delete_ip(ip_address):
  79.     """
  80.     删除无效的ip
  81.     :param ip_address:
  82.     :return:
  83.     """
  84.     connect = pymysql.Connect(
  85.         host='123.59.74.160',
  86.         port=3306,
  87.         user='gtcom',
  88.         passwd='admin@gt.com1',
  89.         db='big_data',
  90.         charset='utf8'
  91.     )
  92.     # connect = pymysql.Connect(
  93.     #     host='10.10.141.235',
  94.     #     port=3306,
  95.     #     user='gtcom',
  96.     #     passwd='admin@gt.com1',
  97.     #     db='big_data',
  98.     #     charset='utf8'
  99.     # )
  100.     # connect = pymysql.Connect(
  101.     #     host='localhost',
  102.     #     port=3306,
  103.     #     user='root',
  104.     #     passwd='',
  105.     #     db='haha',
  106.     #     charset='utf8'
  107.     # )
  108.     cursor = connect.cursor()
  109.     sql = """
  110.         delete from ip_proxy where ip_address='{}'
  111.         """.format(ip_address)
  112.     cursor.execute(sql)
  113.     connect.commit()
  114.     cursor.close()
  115.     connect.close()


快速回复
限100 字节
批量上传需要先选择文件,再选择上传
 
上一个 下一个