J
J
jecer_inside2022-01-10 21:05:43
Python
jecer_inside, 2022-01-10 21:05:43

How to open all links from a file?

There is a file with links to the site (pagination is not suitable)
How to follow these links to get a specific element?
With BeautifulSoup, picking up an element is not a problem.
I can't set up a loop to go through all the links instead of just one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2022-01-10
@jecer_inside

Very simple.
main.py:

import requests
from bs4 import BeautifulSoup

with open('links.txt') as file:
  links = file.read().splitlines()

for link in links:
  response = requests.get(link)
  soup = BeautifulSoup(response.text, 'html.parser')
  print(soup.title)
links.txt:
https://qna.habr.com/q/1099328
https://qna.habr.com/q/1099326
https://qna.habr.com/q/1099320
https://qna.habr.com/q/1099316
Result:
<title>Как удалить одинаковые ключи из словаря Python? — Хабр Q&amp;A</title>
<title>Как открыть все ссылки из файла? — Хабр Q&amp;A</title>
<title>Как отсортировать список файлов? — Хабр Q&amp;A</title>
<title>Как создать "перезапуск кода" на Python? — Хабр Q&amp;A</title>

Application:
Break the code into functions, add error handling to your liking.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question