Как ускорить парсинг BeautifulSoup через мультипроцесс?
Может кто подсказать, как можно ускорить процесс для ускорения парсинга?
В примере около миллион ссылок из сайта vimeo.com...
Мой код:
import sys
import requests
from bs4 import BeautifulSoup
import time
def main():
time_start = time.perf_counter()
with open('urls1.txt', 'r') as input_file:
with open('result1.txt', 'w', encoding='utf-8') as output_file:
for n, url in enumerate(input_file, 1):
url = url.rstrip('\n')
if not url:
continue
try:
r = requests.get(url, headers={'referer': 'null', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)'})
soup = BeautifulSoup(r.text, 'html.parser')
title = soup.select('head > title')[0].text
debug_str = f'Строка {n} - {url} - OK'
except:
title = f'ERROR: {sys.exc_info()[0]}'
debug_str = f'Строка {n} - {url} - ERROR: {sys.exc_info()[0]}'
output_file.writelines(f'{url}\t{title}\n')
print(debug_str)
time_end = time.perf_counter()
print("Закончено за ", "%.2f" % (time_end - time_start), 'сек.')
if __name__ == '__main__':
main()