Sobes.tech
Junior — Senior
48

Повышение эффективности скрипта

Условие задачи

Необходимо понять, что делает представленный фрагмент и предложить пути его ускорения.

import requests as req
from bs4 import BeautifulSoup

input_file = open('links.txt', 'r')
titles = []
line_idx = 0

for line in input_file:
    line_idx += 1
    urls_list = line.split(" ")
    urls_list[1] = urls_list[1].replace('\n', '')

    for link in urls_list:
        response = req.get(link)

        if response.status_code != 200:
            print("error status code get request on ", link, " is ", response.status_code)
            continue

        html = response.text
        parser = BeautifulSoup(html, 'lxml')
        page_title = parser.title.string
        titles.append(page_title)

    if len(titles) == 2:
        if titles[0] != titles[1]:
            print("In the line under the number ", line_idx, " two mismatched titles")
        else:
            print("In the line under the number ", line_idx, " OK")
        titles.clear()
    else:
        print("In the line under the number ", line_idx, " one or both links are not working")

input_file.close()