0

Подскажите пожалуйста, как на каждую ссылку из txt файла сделать запрос и вернуть html с каждого адреса. Вот пример кода.

def data():
    f = open('links.txt', 'r')
    url_list = [line.strip() for line in f.readlines()]
    

    # print(links)
    s = requests.Session()
      
    for url in url_list():
        response = s.get(url=url, headers=headers)

        soup = BeautifulSoup(response.text, 'lxml')

        article = soup.find('div', class_ = 'page-title').find_next('h1').get_text() 
1
  • Если что, каждая ссылка на новой строке
    – ezios23
    13 авг 2022 в 20:53

1 ответ 1

1
def data():
    links = []
    with open('links.txt', encoding='utf8') as file:
        while True:
            line = file.readline()
            if not line:
                break
            line = line.replace('\n', '')
            links.append(line)

    with requests.Session() as se:
        for link in links:
            resp = se.get(link)

            ...


data()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.