Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
import requests with open('urls.txt') as urls: for url in urls: r = requests.get(url) print r.status_code
代码似乎有问题,“urls.txt”行包括“http://”,我认为由于这些脚本无法正常工作,因为我在网站在线时收到404了400状态代码!我怎样才能让网址出现在状态码旁边的终端中?
404
400
您要剥离url,它包括文件中的换行符:
url
import requests with open('urls.txt') as urls: for url in urls: url = url.strip() r = requests.get(url) print url, r.status_code
通过使用.strip(),您可以从字符串的开头和结尾删除空格(空格、制表符、换行符等)。
.strip()
要打印带有状态代码的 URL,只需将其添加到print语句中。
print