码迷,mamicode.com
首页 > 编程语言 > 详细

使用Python自动获取可用代理列表

时间:2015-06-12 01:10:08      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:python 代理 beautifulsoup

        今天闲来无事,随便写的一个从代理发布网站上提取可用代理列表的脚本。运行后,可以获取 http://cn-proxy.com/ 发布的可用代理ip和端口的列表。

        运行效果如下:

技术分享

         源代码如下,请指教:

# -*- coding: utf-8 -*-
# Python:      2.7.8
# Platform:    Windows
# Author:      wucl
# Program:     从代理网站获取可用代理
# History:     2015.6.11


import urllib2, re
from bs4 import BeautifulSoup

def get_proxies(url):
    """
    从代理网站获取可用代理ip地址列表并返回
    """
    resp = urllib2.urlopen(url)
    html = resp.read()
    soup = BeautifulSoup(html)
    contents = soup.find_all(‘tr‘)
    regex = re.compile(‘\d+‘)
    proxies = []
    for each in contents:
        sock = each.find_all(‘td‘)
        if sock:
            ip = sock[0].text
            port = sock[1].text
            if re.findall(regex, ip):
                proxy = ‘%s:%s‘ %(ip, port)
                proxies.append(proxy)
    return proxies



if __name__ == ‘__main__‘:
    url = ‘http://cn-proxy.com/‘
    proxies = get_proxies(url)
    print proxies


本文出自 “载酒仗剑江湖行” 博客,请务必保留此出处http://wucl202000.blog.51cto.com/4687508/1661036

使用Python自动获取可用代理列表

标签:python 代理 beautifulsoup

原文地址:http://wucl202000.blog.51cto.com/4687508/1661036

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!