Pacchetto Python per Spider multi-thread con supporto proxy?
-
06-07-2019 - |
Domanda
Invece di usare solo urllib qualcuno conosce il pacchetto più efficiente per il download rapido e multithread di URL che possono operare tramite proxy HTTP? Conosco alcuni come Twisted, Scrapy, libcurl ecc. Ma non ne so abbastanza su di loro per prendere una decisione o anche se possono usare i proxy ... Qualcuno sa quale sia il migliore per i miei scopi? Grazie!
Soluzione
è semplice implementarlo in Python.
La funzione urlopen () funziona in modo trasparente con i proxy che lo fanno non richiede autenticazione. In un Unix o ambiente Windows, impostare il http_proxy, ftp_proxy o gopher_proxy variabili di ambiente in un URL che identifica prima il server proxy avviare l'interprete 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()
Altri suggerimenti
di solito i proxy filtrano i siti Web in modo categorico in base alla modalità di creazione del sito Web. È difficile trasmettere dati attraverso proxy basati su categorie. Ad esempio, YouTube è classificato come flussi audio / video, pertanto YouTube è bloccato in alcuni luoghi in particolare le scuole. Se si desidera ignorare i proxy e ottenere i dati da un sito Web e inserirli nel proprio sito Web originale come un sito Web dot com che può essere registrato. Quando si effettua e si registra il sito Web, classificare il sito Web come qualsiasi cosa si desideri.