This Python code here reads list.txt which contains websites links and then extract URLS from webarchive.org from those websites, and writes them to urls.txt. What I want is to extract ONLY lines that contains specific "WORD". As I see, my code extract all lines if a specific "WORD" exist in one line.
Can anyone explain why? Thank you in advance!
The code:
urls = []
with open("list.txt", "r") as f_in:
for line in map(str.strip, f_in):
if line == "":
continue
urls.append(line)
archive_url = "http://web.archive.org/cdx/search/cdx?url=*.{}&output=text&fl=original&collapse=urlkey"
with open("url.txt", "w") as f_out:
for url in urls:
r = requests.get(archive_url.format(url))
if 'WORD' in archive_url:
print(r.text, file=f_out)
print("\n", file=f_out)
I tried to replace if 'WORD' in archive_url: with if 'WORD' in url: but it doesn't write anything to TXT!
I don't know how to print only the LINE which contain "WORD"