user1985563
user1985563

Reputation: 197

Requests.get module python getting 404-400

import requests
with open('urls.txt') as urls:
    for url in urls:
        r = requests.get(url)
        print r.status_code

The code has appears to have a problem, the "urls.txt" lines include "http://" and I think because of those the script isn't working because I receive 404 and 400 status codes while the websites are online! And how can I have the urls appear in terminal next to the status code?

Upvotes: 0

Views: 316

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121534

You want to strip the url, it includes the newline from the file:

import requests
with open('urls.txt') as urls:
    for url in urls:
        url = url.strip()
        r = requests.get(url)
        print url, r.status_code

By using .strip() you remove whitespace (spaces, tabs, newlines, etc.) from the start and end of the string.

To print the URL with the status code, simply add it to the print statement.

Upvotes: 1

Related Questions