python - 从文本文件中检查 URL 文件是否存在
问题描述
我正在尝试做以下事情:
1 - open a text file containing a list with URLs (http://example.com). <br>
2 - read the text file and check if the path existe. <br>
3 - write the results back in another text file.
我尝试了以下代码:
import urllib2
file = open('file.txt', 'r')
search = urllib2.urlopen(file + "/js/tools.js")
if search.code == 200:
print "Exists!"
我感谢提供的任何帮助。
解决方案
考虑到您有文件Filename
,因为链接是逐行存储的
import requests
with open(filename,'r') as file, open(filename2,'w+') as file2:
for url in file.readlines():
check = requests.get(url)
if check.ok:
file2.write(url)
推荐阅读
- jquery - 在 div 中更改 div 的样式
- javascript - AJAX 调用中的 Crypto-JS 纯文本解密错误
- spring - Hikari + Hibernate + Postgres:无法连接
- windows - 如何以编程方式设置 SourceMask 大小?
- laravel - 将数据从控制器带到 vue.js 的正确方法是什么?Laravel-vue.js
- css - HTML/CSS/JavaScript 方法,用于按行和列调整单元格大小
- reactjs - React useState 似乎不适用于对象
- python - 如何将嵌套字典呈现为表格
- python - Tkinter - 将文本框中的信息插入到 python 脚本中的特定位置
- python - 过滤大量 pyspark 数据帧,成功执行后 .show() 方法出错