检查文本文件中是否存在URL文件

时间:2019-05-31 05:10:32

标签: python python-3.x python-2.7 urllib2

我正在尝试做以下事情:

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!"

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

考虑到您有文件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)