Question

trying to read a file with urls and extract the status_code with python requests module.

this does not work, or gives me a wrong status code:

import requests

f = open('urls.txt','r')

for line in f:
  r =  requests.head(line)
  print r.status_code

  if r.status_code == requests.codes.ok:
    print "ok";

but if I do it manually (without file read) it works.

import requests

r =  requests.head('https://encrypted.google.com/')
print r.status_code

if r.status_code == requests.codes.ok:
  print "ok";
Was it helpful?

Solution

Do a line.strip() before making the head call. There is a possibility that the carriage return and/or line feed characters are part of the line you just read.

Like this:

for line in f:
    r =  requests.head(line.strip())
    print r.status_code
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top