Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to pass contents of a file line by line to requests module in python

I have written this script that will retrieve the contents of a web page.

import requests
import bs4

with requests.session() as r:
    r = requests.get("https://www.example.com")
    response = r.text
    print(response)

However, I have a list of URLs in a text file. Is there any way I can pass the contents of this file directly to requests.get() instead of typing each one manually.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just put it all in a loop.

import requests
import bs4

text_file_name = "list_of_urls.txt"

with requests.session() as session:
    with open(text_file_name) as file:
        for line in file:
            url = line.strip()
            if url:
                resp = session.get(url)
                response = resp.text
                print(response)

note: you weren’t using the requests session object, so fixed that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading