[英]Why whois parsed output with python is not written to a text file?
I am writing the below python script to parse whois data for a list of domain names that I have in a separate file. 我正在编写以下python脚本来解析whois数据,以获取我在单独文件中拥有的域名列表。 Here is the script: 这是脚本:
from __future__ import print_function, division
import whois
from publicsuffix import PublicSuffixList
file = '/data/personal/Mae-git-domains.txt'
output = '/data/personal/whois-Mae.txt'
psl = PublicSuffixList()
f = open (file,'r')
for line in f:
url = line.split(' ')[1]
domain = url.split('http://')[1].split('/')[0]
try:
w =whois.whois(domain)
results = domain,w.creation_date,w.expiration_date
with open (output,'wb') as m:
m.write(results.text)
# print ("%s,%s,%s" % (domain,w.creation_date,w.expiration_date), file = m )
# print >> m ,"%s|%s|%s" % (domain,w.creation_date,w.expiration_date)
# m.write("{0},{0},{0}".format(domain,w.creation_date,w.expiration_date))
except:
# print ("%s,%s,%s" % (domain,[],[]), file = m )
pass
It gets printed when I print it on the terminal but not when I am trying to either write or print it into a text file (the other printing/writing attempts are in the code as comments). 当我在终端上打印它时,它会被打印,但是当我尝试将其写入或打印到文本文件中时,它不会被打印(其他打印/写入尝试在代码中作为注释)。 Does any one have an idea why this is happening? 有谁知道为什么会这样吗?
要将其写入文本文件,可以使用m.write(str(results.text))
,因为它的类型为unicode,因此您基本上需要强制将其作为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.