使用Python自動擷取可用代理列表

來源:互聯網
上載者:User

標籤:python 代理 beautifulsoup

        今天閑來無事,隨便寫的一個從代理髮布網站上提取可用代理列表的指令碼。運行後,可以擷取 http://cn-proxy.com/ 發布的可用代理ip和連接埠的列表。

        運行效果如下:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/56/wKioL1V5lq2iUMpOAAIMScFn8jU939.jpg" title="可用代理列表" alt="wKioL1V5lq2iUMpOAAIMScFn8jU939.jpg" />

         原始碼如下,請指教:

# -*- coding: utf-8 -*-# Python:      2.7.8# Platform:    Windows# Author:      wucl# Program:     從代理網站擷取可用代理# History:     2015.6.11import urllib2, refrom bs4 import BeautifulSoupdef 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 proxiesif __name__ == ‘__main__‘:    url = ‘http://cn-proxy.com/‘    proxies = get_proxies(url)    print proxies


本文出自 “載酒仗劍江湖行” 部落格,請務必保留此出處http://wucl202000.blog.51cto.com/4687508/1661036

使用Python自動擷取可用代理列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.