python - 我在终端上运行时得到 404,在空闲时得到 200
问题描述
代码:
import requests
file = open("test.txt")
for url in file:
response = requests.get(url)
print(response.status_code)
---------test.txt------------
https://www.google.com
终端中的 O/p:
404
当我在空闲时使用它时:
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> response = requests.get("https://google.com")
>>> response.status_code
200
解决方案
您文件中的每个url
文件末尾都有一个换行符。试试看strip()
:
for url in file:
response = requests.get(url.strip())
推荐阅读
- lisp - AutoCAD LISP 创建
- angular - Angular Highcharts 中的日期类型 xAxis 仍然允许重复的“日期”值
- python - 当我使用 MITSceneParsing 数据实现 FCN 时,出现此错误 [TypeError: 'NoneType' object is not subscriptable]
- python - 过滤到文本文件后保存列表输出
- javascript - 我在 MEAN 堆栈中的 To Do App 开发需要帮助
- heroku - 无法在 Heroku 上安排备份?
- gitlab - GitLab.com(代码)和托管 GitLab 服务器(Wiki/Issues/CI/CD)之间的集成
- javascript - 单击几下后如何禁用按钮功能?
- halide - 是否可以为卤化物管道创建具有预定义大小的 ImageParam?
- python - 将 .ui 文件中的小部件加载到由单独的 .ui 文件定义的窗口中