使用Python文本文件检查soundcloud上的死链接

时间:2016-09-19 18:08:29

标签: python testing hyperlink

Python 2.7

Windows 10 x64

我有一个完整的soundcloud链接的.txt文件,并想知道我如何使用这个txt文件作为输入并循环检查标题显示的错误的链接。然后打印那些没有给出错误的内容。

这就是我所拥有的,但它一直给我404.

# Test Dead Link
# https://soundcloud.com/nightsanity-793590747/the-xx-intro
# Working Link
# https://soundcloud.com/madeleinepeyroux/everything-i-do-gonna-be-funky

import requests
filename = 'data.txt'
with open(filename) as f:
    data = f.readlines()

for row in data:
    r = requests.get(row)
    r.status_code
    if r.status_code == 404:
        print 'The Link is Dead'
    else:
        print 'The Link is Alive' 

1 个答案:

答案 0 :(得分:0)

问题是由行变量末尾的回车/换行引起的。

使用

r = requests.get(row.strip())

摆脱空白以及网址的开头和结尾。您可能还必须处理异常:

# Test Dead Link
# https://soundcloud.com/nightsanity-793590747/the-xx-intro
# Working Link
# https://soundcloud.com/madeleinepeyroux/everything-i-do-gonna-be-funky

import requests
filename = 'data.txt'
with open(filename) as f:
    data = f.readlines()

for row in data:
    print row

    try:
        r = requests.get(row.strip())
        print 'The Link is Alive'
    except:
        print 'The Link is Dead'
    print

有关如何处理请求异常的更多信息,请访问here

适用于以下文件text.txt:

http://www.cnn.com
http://www.jkawhegcbkqjwzetrc.com
http://www.google.com