Of course you can. In passing some modifications to your code:
from selenium import webdriver
from bs4 import BeautifulSoup
with open("listado.txt", "r") as delFichero:
browser = webdriver.Firefox()
for linea in delFichero:
url = linea.strip()
browser.get(url)
content = browser.page_source
soup = BeautifulSoup(content, "html.parser")
print(url)
browser.quit()
First of all we used a contextmanager
to handle the reading of the file the following way:
with open("listado.txt", "r") as delFichero
This way is much safer because we can forget to close the file, in fact you forgot to add the close. The contextmanager
knows when you have stopped reading in this case and automatically closes the file.
One detail, by doing this: url = linea.strip()
we remove the line breaks that we read from the file, in your code it seems that it is not necessary but it is always a good detail.
The instantiation of BeautifulSoup
has changed, and should be done like this: BeautifulSoup(content, "html.parser")
specifying the appropriate "parser".
If you analyze the new code you will see that now the browser.quit()
is out of the cycle, so the browser will not close until it has completed it.