1

Python 2.7

Windows 10 x64

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.

This what I have but it keeps giving me 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 Answers1

0

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

Use

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

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

# 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

Some more information how to handle exceptions for requests can be found here.

Works well with the following file text.txt:

http://www.cnn.com
http://www.jkawhegcbkqjwzetrc.com
http://www.google.com
Community
  • 1
  • 1
tfv
  • 6,016
  • 4
  • 36
  • 67