具有代理支持的多线程蜘蛛Python包?

Coo*_*ies 1 python proxy multithreading pool web-crawler

有没有人知道用于快速,多线程下载可以通过http代理操作的URL的最有效的包,而不仅仅是使用urllib?我知道一些如Twisted,Scrapy,libcurl等,但我不知道他们做出决定,或者即使他们可以使用代理......任何人都知道最适合我的目的?谢谢!

小智 17

在python中实现它很简单.

urlopen()函数与不需要身份验证的代理透明地工作.在Unix或Windows环境中,将http_proxy,ftp_proxy或gopher_proxy环境变量设置为在启动Python解释器之前标识代理服务器的URL

# -*- coding: utf-8 -*-

import sys
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
from Queue import Queue, Empty
from threading import Thread

visited = set()
queue = Queue()

def get_parser(host, root, charset):

    def parse():
        try:
            while True:
                url = queue.get_nowait()
                try:
                    content = urlopen(url).read().decode(charset)
                except UnicodeDecodeError:
                    continue
                for link in BeautifulSoup(content).findAll('a'):
                    try:
                        href = link['href']
                    except KeyError:
                        continue
                    if not href.startswith('http://'):
                        href = 'http://%s%s' % (host, href)
                    if not href.startswith('http://%s%s' % (host, root)):
                        continue
                    if href not in visited:
                        visited.add(href)
                        queue.put(href)
                        print href
        except Empty:
            pass

    return parse

if __name__ == '__main__':
    host, root, charset = sys.argv[1:]
    parser = get_parser(host, root, charset)
    queue.put('http://%s%s' % (host, root))
    workers = []
    for i in range(5):
        worker = Thread(target=parser)
        worker.start()
        workers.append(worker)
    for worker in workers:
        worker.join()
Run Code Online (Sandbox Code Playgroud)