Rahul
Rahul

Reputation: 109

How to use request and read urls from a txt file

Im writing a piece of code where I have a list of urls in a file and Im using requests to go through the file and do a GET request and print the status code but from what i have written I am not getting any output

import requests
with open('demofile.txt','r') as http:
    for req in http:
     page=requests.get(req)
     print page.status_code

Upvotes: 0

Views: 1629

Answers (1)

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

I see two problems, one is that you forgot to indent the lines after the for loop and the second one is that you failed to remove the last new lines \n (supposing that urls are separated in different lines)

import requests

with open('deleteme','r') as urls:
    for url in urls.readlines():
        req = url.strip()
        print(req)
        page=requests.get(req)
        print(page.status_code)

Upvotes: 2

Related Questions