gpt4 book ai didi

Python简单字符串格式化错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:57 25 4
gpt4 key购买 nike

这是我的脚本,它应该解析 .txt 文件中的域列表(每个域由返回分隔),将它们分成单独的域名,向带有域名的 whois 站点发送请求,检查响应查看它是否可用,如果可用,则将其写入新文件。所以我得到了一个只有可用名称的列表。

问题?这很简单我只是不太了解语言,我不知道如何以字符串格式获取域名,以便对 whois 站点的请求是这样的:

http://whois.domaintools.com/google.com

显然 %s 没有工作。

代码:

#!/usr/bin/python
import urllib2, urllib
print "Domain Name Availability Scanner."
print "Enter the Location of the txt file containing your list of domains:"
path = raw_input("-->")

wordfile = open(path, "r")
words = wordfile.read().split("n")
words = map(lambda x: x.rstrip(), words)
wordfile.close()

for word in words:
req = urllib2.Request("http://whois.domaintools.com/%s") % (word)
source = urllib2.urlopen(req).read()
if "This domain name is not registered" in source:
f = open("success.txt", "a")
f.write("%s\n") % (word)
f.close()
break

终端错误:

python domain.py
Domain Name Availability Scanner.
Enter the Location of the txt file containing your list of domains:
-->a.txt
Traceback (most recent call last):
File "domain.py", line 13, in <module>
req = urllib2.Request("http://whois.domaintools.com/%s") % (word)
TypeError: unsupported operand type(s) for %: 'instance' and 'str'

最佳答案

修复括号:

req = urllib2.Request("http://whois.domaintools.com/%s" % (word))

还有:

f.write("%s\n" % word)

:)

关于Python简单字符串格式化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11087000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com