[英]python requests read from file status_code wrong
trying to read a file with urls and extract the status_code with python requests module. 尝试读取带有url的文件并使用python请求模块提取status_code。
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";
Do a line.strip()
before making the head
call. 做一个
line.strip()
使前head
的呼叫。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.