Check for Dead Links on soundcloud using text file with Python
<p>Python 2.7</p>

<h2>Windows 10 x64</h2>

<p>I have a .txt file full of soundcloud links and was wondering how I can use this txt file as input and loop through the links checking for the error       that the header displays. And then printing those that do not give the error.</p>

<p>This what I have but it keeps giving me 404.</p>

<pre><code># 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' 
</code></pre>

<p>The problem is caused by carriage return / line feed at the end of your row variable.</p>

<p>Use</p>

<pre><code>r = requests.get(row.strip())
</code></pre>

<p>to get rid of blanks and the beginning and end of your url. You may also have to handle exceptions:</p>

<pre><code># 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
</code></pre>

<p>Some more information how to handle exceptions for requests can be found <a href="http://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module">here</a>.</p>

<p>Works well with the following file text.txt:</p>

<pre><code>http://www.cnn.com
http://www.jkawhegcbkqjwzetrc.com
http://www.google.com
</code></pre>

python testing hyperlink