Медленная загрузка с Python Script & TOR (включая исходный код)
Я пытаюсь скачать html-страницы с моим скриптом Python и прокси-сервером TOR. Работает хорошо. Но очень медленно и код не организован, поэтому мой IP-адрес обновляется большую часть времени, а загрузка страниц происходит очень часто. Как я могу ускорить загрузку с помощью TOR? Как я могу организовать эффективность кода.
Два сценария есть. Script1 выполняется для загрузки html-страниц с веб-сайта, и после получения блокировки с веб-сайта необходимо выполнить Script2 для обновления IP с помощью прокси-сервера TOR. Итак... IP блокируется через несколько секунд. Должен ли я снизить поток? Как? Пожалуйста, помогите мне ускорить процесс. Я получаю только 300-500 HTML-страниц в час.
Вот мой полный код Script1:
# -*- coding: UTF-8 -*-
import os
import sys
import socks
import socket
import subprocess
import time
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, '127.0.0.1', 9050, True)
socket.socket = socks.socksocket
import urllib2
class WebPage:
def __init__(self, path, country, url, lower=0,upper=9999):
self.dir = str(path)+"/"+ str(country)
self.dir =os.path.join(str(path),str(country))
self.url = url
try:
fin = open(self.dir+"/limit.txt",'r')
limit = fin.readline()
limits = str(limit).split(",")
lower = int(limits[0])
upper = int(limits[1])
fin.close()
except:
fout = open(self.dir+"/limit.txt",'wb')
limits = str(lower)+","+str(upper)
fout.write(limits)
fout.close()
self.process_instances(lower,upper)
def process_instances(self,lower,upper):
try:
os.stat(self.dir)
except:
os.mkdir(self.dir)
for count in range(lower,upper+1):
if count == upper:
print "all downloaded, quitting the app!!"
break
targetURL = self.url+"/"+str(count)
print "Downloading :" + targetURL
req = urllib2.Request(targetURL)
try:
response = urllib2.urlopen(req)
the_page = response.read()
if the_page.find("Your IP suspended")>=0:
print "The IP is suspended"
fout = open(self.dir+"/limit.txt",'wb')
limits = str(count)+","+str(upper)
fout.write(limits)
fout.close()
break
if the_page.find("Too many requests")>=0:
print "Too many requests"
print "Renew IP...."
fout = open(self.dir+"/limit.txt",'wb')
limits = str(count)+","+str(upper)
fout.write(limits)
fout.close()
subprocess.Popen("C:\Users\John\Desktop\Data-Mine\yp\lol\lol2.py", shell=True)
time.sleep(2)
subprocess.call('lol1.py')
if the_page.find("404 error")>=0:
print "the page not exist"
continue
self.saveHTML(count, the_page)
except:
print "The URL cannot be fetched"
execfile('lol1.py')
pass
#continue
raise
def saveHTML(self,count, content):
fout = open(self.dir+"/"+str(count)+".html",'wb')
fout.write(content)
fout.close()
if __name__ == '__main__':
if len(sys.argv) !=6:
print "cannot process!!! Five Parameters are required to run the process."
print "Parameter 1 should be the path where to save the data, eg, /Users/john/data/"
print "Parameter 2 should be the name of the country for which data is collected, eg, japan"
print "Parameter 3 should be the URL from which the data to collect, eg, the website link"
print "Parameter 4 should be the lower limit of the company id, eg, 11 "
print "Parameter 5 should be the upper limit of the company id, eg, 1000 "
print "The output will be saved as the HTML file for each company in the target folder's country"
exit()
else:
path = str(sys.argv[1])
country = str(sys.argv[2])
url = str(sys.argv[3])
lowerlimit = int(sys.argv[4])
upperlimit = int(sys.argv[5])
WebPage(path, country, url, lowerlimit,upperlimit)
1 ответ
TOR очень медленный, поэтому следует ожидать, что вы не получаете столько страниц в час. Однако есть несколько способов ускорить его. В частности, вы можете включить сжатие GZIP для urllib (см. Этот вопрос, например), чтобы немного повысить скорость.
TOR как протокол имеет довольно низкую пропускную способность, потому что данные необходимо ретранслировать несколько раз, и каждый ретранслятор должен использовать свою пропускную способность для вашего запроса. Если данные передаются 6 раз - довольно вероятное число - вам потребуется 6-кратная пропускная способность. Сжатие GZIP может сжимать HTML до (в некоторых случаях) ~10% от исходного размера, что, вероятно, ускорит процесс.