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

有帮助吗?

解决方案

在python中实现它很简单。

  

urlopen()函数有效   透明地使用代理   不需要身份验证。在Unix中   或Windows环境,设置   http_proxy,ftp_proxy或gopher_proxy   环境变量到URL   之前标识代理服务器   启动Python解释器

# -*- 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()

其他提示

通常代理会根据网站的创建方式过滤网站。基于类别通过代理传输数据很困难。例如,youtube被归类为音频/视频流,因此youtube在一些地方特别是学校被封锁。 如果您想绕过代理并从网站上获取数据并将其放入您自己的正版网站,例如可以向您注册的.com网站。 当您制作和注册网站时,将您的网站分类为您想要的任何内容。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top