简体   繁体   English

Requests.get模块python获取404-400

[英]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! 该代码似乎有问题,“ urls.txt”行中包含“ http://”,我认为由于这些脚本无法正常工作,因为在网站在线时我收到了404400状态代码! And how can I have the urls appear in terminal next to the status code? 网址如何显示在状态代码旁边的终端中?

You want to strip the url , it includes the newline from the file: 您要剥离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

By using .strip() you remove whitespace (spaces, tabs, newlines, etc.) from the start and end of the string. 通过使用.strip()您可以从字符串的开头和结尾删除空格(空格,制表符,换行符等)。

To print the URL with the status code, simply add it to the print statement. 要打印带有状态代码的URL,只需将其添加到print语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM