Home > Mobile >  Why does Python output an empty list when it should have output a list of URLs?
Why does Python output an empty list when it should have output a list of URLs?

Time:02-03

I ran the script below to grab text from a Pastebin text file and turn it into a list. Unfortunately, it's printing an empty list. Can anyone help me diagnose why this is the case? Thank you!

Command typed:

script.py -id 049YJMdV

Script

import requests, re, argparse

parser = argparse.ArgumentParser()
parser.add_argument('-id','--id', required=False)
args = vars(parser.parse_args())
id = args['id']

link = "https://web.archive.org/web/20150611011149/http://pastebin.com/raw.php?i={}".format(id)
data = []

links = requests.get(link).text
urls = re.findall(r'https?://[^\s<>"] [|www\.^\s<>"] ', links)


print(data)

Output

[]

CodePudding user response:

The array is getting printed empty because you're defining

data = []

without actually appending anything to it at all. Therefore, its behaving as expected because nothing has been added to it and prints an empty list.

Also, you have the id as not required. However, it will throw an exception since no id is provided in the url. Would double check that as well.

  •  Tags:  
  • Related