Could you please be a little more clear about what you want?
Here is a possible answer which might or might not be what you want:
from bs4 import BeautifulSoup
import requests
with open('yourfilename.txt', 'r') as url_file:
for line in url_file:
u = line.strip()
response = requests.get(u)
data = response.text
soup = BeautifulSoup(data,'lxml')
The file was opened with the open()
function; the second argument is 'r'
to specify we're opening it in read-only mode. The call to open()
is encapsulated in a with
block so the file is automatically closed as soon as you no longer need it open.
The strip()
function removes trailing whitespace (spaces, tabs, newlines) at the beginning and end of every line, for instant ' https://stackoverflow.com '.strip()
becomes 'https://stackoverflow.com'
.