Python简单字符串格式

2024-05-23 20:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的脚本,它应该在一个.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

终端错误:

^{pr2}$

Tags: 文件thetxtcomhttp列表站点whois
3条回答

您需要使用:

req = urllib2.Request("http://whois.domaintools.com/%s" % word)
# ...
f.write("%s\n" % word)

使用:

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

请查看此链接,它应该解释此格式的工作原理:http://docs.python.org/release/2.5.2/lib/typesseq-strings.html

修复括号:

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

以及:

^{pr2}$

:)

相关问题 更多 >