关于ip代理的三个模块
获取ip,然后随机选择ip,删除无用的ip
#!/usr/bin/env python # coding:utf8 # author:Z time:2018/8/16 import random import pymysql import requests def get_ip(): """ 获取ip代理池里的ip :return: """ connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', password='', db='haha', charset='utf8' ) # connect = pymysql.Connect( # host='10.10.141.235', # port=3306, # user='gtcom', # passwd='admin@gt.com1', # db='big_data', # charset='utf8' # ) sql1 = """ select ip_address from ip_proxy """ sql2 = """ select port from ip_proxy """ sql3 = """ select type_ from ip_proxy """ cursor = connect.cursor() sql4 = """ select count(*) from ip_proxy """ cursor.execute(sql4) a = cursor.fetchall() # ((0,),) if a[0][0] == 0: # 判断数据库是否为空 print('没有数据') # 调用付费代理ip html = requests.get( '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=') pay_ip_proxy = html.text.strip() s_ip_proxy = pay_ip_proxy.split(':') ip_addresses = s_ip_proxy[0] ports = s_ip_proxy[1] type_s = 'HTTP' return ip_addresses,ports,type_s else: cursor.execute(sql1) ip_addresses = cursor.fetchall() # ip_address=random.choice(ip_addresses)[0] cursor.execute(sql2) ports = cursor.fetchall() # port=random.choice(ports)[0] cursor.execute(sql3) type_s = cursor.fetchall() # type_=random.choice(type_s)[0] return ip_addresses,ports,type_s ip_addresses,ports,type_s = get_ip() def change_ip(): """ 随机切换ip :return: """ ip_address = random.choice(ip_addresses)[0] port = random.choice(ports)[0] type_ = random.choice(type_s)[0] proxies = { type_: type_ + '://' + ip_address + ':' + port } return proxies,ip_address def delete_ip(ip_address): """ 删除无效的ip :param ip_address: :return: """ connect = pymysql.Connect( host='123.59.74.160', port=3306, user='gtcom', passwd='admin@gt.com1', db='big_data', charset='utf8' ) # connect = pymysql.Connect( # host='10.10.141.235', # port=3306, # user='gtcom', # passwd='admin@gt.com1', # db='big_data', # charset='utf8' # ) # connect = pymysql.Connect( # host='localhost', # port=3306, # user='root', # passwd='', # db='haha', # charset='utf8' # ) cursor = connect.cursor() sql = """ delete from ip_proxy where ip_address='{}' """.format(ip_address) cursor.execute(sql) connect.commit() cursor.close() connect.close()

浙公网安备 33010602011771号