scrapy (IP代理)从数据库中取出ip判断是否可用
发布时间:2019-01-10
import pymysql
import requests
conn = pymysql.connect(host="localhost",user="root",passwd="root",db="leymax",charset="utf8")
cursor = conn.cursor()
classGetIp(object):
# 删除无效ip
defdelete_ip(self,ip):
sql ="delete from xiciproxy where ip='{0}'".format(ip)# format() 格式化字符串
cursor.execute(sql)
conn.commit()
return True
# 判断ip是否可用
defjudge_ip(self,type,ip,port):
http_url ="http://www.baidu.com"
proxy_url ="{0}://{1}:{2}".format(type,ip,port)
print("url:"+ proxy_url)
try:
proxy_dict = {
type: proxy_url, # type 为https 或者http 数据库存储的
}
response = requests.get(http_url,proxies=proxy_dict)
exceptExceptionase:
self.delete_ip(ip)
print("invalid ip and port")
return False
code = response.status_code
if code >=200 and code <300:
print("effective ip")
return True
else:
print("invalid ip and port")
self.delete_ip(ip)
return False
defget_random_ip(self):# 从数据库中随机获取一个可用的ip
random_sql ="""
SELECT type, ip, port FROM xiciproxy
ORDER BY RAND()
LIMIT 1
"""
result = cursor.execute(random_sql)
for ip_info in cursor.fetchall():
type = ip_info[0]
ip = ip_info[1]
port = ip_info[2]
judge_re =self.judge_ip(type,ip,port)
ifjudge_re:
return"{0}://{1}:{2}".format(type,ip,port)
else:
return self.get_random_ip()
if__name__ =="__main__":
get_ip = GetIp()
print(get_ip.get_random_ip())
热门文章
nginx+php 开启PHP错误日志
行业早报2019-01-15为什么你说了很多遍,对方还是不听? 2018-09-25
行业早报2019-01-15【Ruby on Rails实战】3.1 宠物之家论坛管理系统介绍
行业早报2019-01-15从凡人到筑基期的单片机学习之路
行业早报2019-01-15jmeter单台大数量并发
行业早报2019-01-15Go在Windows下开发环境搭建
行业早报2019-01-15ES-科普知识篇
行业早报2019-01-15Hbase 之 由 Zookeeper Session Expired 引发的 HBASE 思考
行业早报2019-01-15谷歌大脑专家详解:深度学习可以促成哪些产品突破?
行业早报2019-01-15EventLoop
行业早报2019-01-15
相关推荐