Reputation: 77
I'm trying to do a script which reads a line (URL) in a text file, checks its code_status
and then print to the user, however, when using for loop it gives wrong code_status
while testing with one URL in the request.get(url)
gives the right one.
Problematic code
import requests
with open('test2.txt', 'r+') as arquivo:
for linhas in arquivo:
url = requests.get(linhas)
print url.status_code
Achieves the right code.
import requests
url = requests.get(URL)
print url.status_code
You guys can test both the txt file and single URL with the following URL: https://api.github.com/user
What am I doing wrong?
Upvotes: 1
Views: 472
Reputation: 859
Most probably your problem is because of the \n
character at the end of each line.
this should fix it:
import requests
with open('test2.txt', 'r+') as arquivo:
for linhas in arquivo:
url = requests.get(linhas.strip())
print url.status_code
Upvotes: 2