'f-string interfering with extracting URLs
tl;dr f-string is messing up the script below. List printed is empty despite the file containing a list of URLs. How can I fix this problem and have Python print out the URLs?
So I have a script below. It downloads a list of URLs, converts it into a list, and then prints it out. Now, for the variable link, there's an f-string. If I keep just one value in the f-string (say I delete fromdate and todate and just keep username), it works just fine. But if I put multiple values in the f-string, the script fails.
COMMAND
script.py -u mrbeast
SCRIPT
import argparse, re, requests
parser = argparse.ArgumentParser()
parser.add_argument('-u','--username', required=False)
parser.add_argument('-from','--fromdate', required=False)
parser.add_argument('-to','--todate', required=False)
args = vars(parser.parse_args())
username = args['username']
fromdate = args['fromdate']
todate = args['todate']
link = "https://web.archive.org/cdx/search/cdx?url=twitter.com/{}/status&matchType=prefix&from={}&to={}".format(username,fromdate,todate)
listy = []
m = requests.get(link).text
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', m)
for b, url in enumerate (urls):
listy.append(f"{b}: {url}")
print(listy)
OUTPUT
[]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
