python - 遍历 url 列表时无法建立连接
问题描述
我想遍历 url 列表并使用请求打印状态代码
这是我的代码
import requests
file = open("urls.txt", "r+")
for urls in file:
list = "https://" + urls
r = requests.get(list)
print(r.status_code)
这是我的 urls.txt
login.spotify.com
hrblog.spotify.com
lon.spotify.com
cms-change.spotify.com
upgrade.spotify.com
mydata.spotify.com
spclient.wg.spotify.com
这是我得到的错误
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='login.spotify.com%0a', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x03141A30>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
解决方案
当您从文件中读取行时,您必须\n
从行尾删除,即。
urls = urls.strip()
顺便说一句: \n
有十六进制代码0a
,你可以在你的网址中看到它host='login.spotify.com%0a'
推荐阅读
- ms-access - 有没有办法通过访问 vba 将位于我的网站目录中的文件复制到我的本地驱动器中
- java - JAVA,如何在 xml 中编辑节点内的文本?
- sql - 按列分组并显示符合条件的列的值
- angular - NG Prime p-multiSelect 禁用选项不起作用
- json - 通过连接两个变量的 Json 对象值
- android - 服务调用 checkSelfPermission 返回 nullPointerException
- jquery - 如何停止 Jquery-Steps?
- javascript - 在服务器端 javascript 上运行查询
- eclipse - Eclipse Project Explorer 显示对 SVN 外部的修改
- docker - 在 Alpine Linux 上运行“chpasswd”时出现“PAM:身份验证失败”