最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python采集代理ip并判斷是否可用和定時(shí)更新的方法

 更新時(shí)間:2018年05月07日 11:05:16   作者:lilongsy  
今天小編就為大家分享一篇Python采集代理ip并判斷是否可用和定時(shí)更新的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

網(wǎng)上有很多免費(fèi)的ip地址,都是可以使用的,但是如果手動(dòng)來(lái)獲取太麻煩,這里通過(guò)Python自動(dòng)抓取,可以批量獲取。

代碼如下:

# -*- coding: utf-8 -*-
import re
import urllib2
import json
import os
import time
import socket

class ProxyIp(object):
  def __init__(self):
    self.path = os.path.split(os.path.realpath(__file__))[0]
  # Get latest proxy ip and download to json
  def update_ip(self):
    print 'Update Ip'
    url = 'http://www.ip3366.net/free/'
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    matches = re.findall(
      ur'(\d+.\d+.\d+.\d+)</td>\s+<td>(\d+)</td>\s+<td>.*?</td>\s+<td>(HTTPS?)</td>',
      response.read(),
      re.I
    )
    ls = []
    for match in matches:
      if self.is_open(match[0], match[1]):
        ls.append({'ip':match[0], 'port':match[1], 'protocol': match[2]})
    with open('%s/ip.json' % self.path, 'w') as f:
      json.dump(ls, f)
    return ls
  # whether the ips is last or old.
  def is_last(self):
    m_time = int(os.path.getmtime('%s/ip.json' % self.path))
    now_time = int(time.time())
    return (now_time - m_time) > 60*60*4 # 4 hours
  @staticmethod
  def is_open(ip, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
      s.connect(ip, int(port))
      return True
    except:
      print 'Faild IP: %s:%s' % (ip, port)
      return False
  def get_proxy_ips(self):
    if not self.is_last():
      return self.update_ip()
    else:
      with open('%s/ip.json' % self.path, 'r') as f:
        return json.load(f)

以上這篇Python采集代理ip并判斷是否可用和定時(shí)更新的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

区。| 平利县| 长泰县| 横山县| 青川县| 谷城县| 卢氏县| 舒兰市| 长沙市| 通州市| 吴旗县| 庄河市| 甘南县| 济源市| 遵化市| 盐山县| 陵川县| 滨海县| 汝州市| 新丰县| 夹江县| 枣庄市| 吉安市| 太湖县| 玉林市| 乡城县| 石城县| 蛟河市| 六枝特区| 民丰县| 和田县| 黎川县| 综艺| 北安市| 东乡族自治县| 滨海县| 保靖县| 汶上县| 新巴尔虎左旗| 南通市| 东丰县|