Each line of the mentioned file is looked very much like JSON encoded dictionary.
So it's a good case for json
module:
import json
with open("text.txt", "r") as fh:
domains = []
for l in fh.readlines():
d = json.loads(l)
domains.append(d["name"])
# some url domains are located in `value` key for the records which have "type":"cname"
if (d["type"] == "cname"): domains.append(d["value"])
print(domains)
The output:
['mail.callfieldcompanion.com', 'reseauocoz.cluster007.ovh.net', 'cluster007.ovh.net', 'ghs.googlehosted.com', 'googlehosted.l.googleusercontent.com', 'isutility.web9.hubspot.com', 'a1049.b.akamai.net', 'plato.mx25.net']
If the input file contains a single line use the following approach:
import json, re
with open("text.txt", "r") as fh:
domains = []
# emulating the list of dictionaries
line = "[" + re.sub(r'\}\s*\{', '},{',fh.read()) + "]"
l = json.loads(line)
for d in l:
domains.append(d["name"])
# some url domains are located in `value` key for the records which have "type":"cname"
if (d["type"] == "cname"): domains.append(d["value"])
print(domains)