[英]How to store multiple values in keys in dictionary in python?
也许我的问题标题与问题内容不同。
statuscode = []
statuscode.append(200)
for x in find_from_sublister(hostname):
x2 = x.strip()
url = "http://"+ x2
try:
req = requests.get(url)
req1 = str(req.status_code) + " " + str(url) + '\n'
req2 = str(req.status_code)
req3 = str(url)
dict = {req2 : req3}
print " \n " + str(req1)
except requests.exceptions.RequestException as e:
print "Can't make the request to this Subdomain " + str(url) + '\n'
for keys, values in dict.iteritems():
print "finding the url's whose status code is 200"
if keys == statuscode[0]:
print values
else:
print "po"
我正在使用代码来做以下一些工作。
for x in find_from_sublister(hostname):
注意:find_from_sublister(hostname)是用于从子列表查找子域的功能。
print " \\n " + str(req1)
[一切顺利,但问题从这里开始]
现在我想要的是分隔具有200个状态代码的URL。
所以我听说那可以通过在python中使用字典来实现。 因此,我尝试使用字典。 如您所见, dict = {req2 : req3}
现在我也列出了值200
的索引
然后,我将键与索引列表进行比较。 这里的keys == statuscode[0]
如果它们匹配,则应打印所有状态代码为200的网址。
但是我得到的结果低于
finding the url's whose status code is 200
po
您会看到值po
的else陈述值,
else:
print "po"
现在的问题是,为什么我要得到这个else值,为什么状态码为200的URL不能呢?
希望我能清楚地向你解释。 等着有人跟我说话。
谢谢
注意:我正在使用Python 2.7版本。
就您而言,您甚至不需要字典。
我试图尽可能地清理代码,包括更多描述性的变量名(同样,重写诸如dict
这样的内置名称也是一个坏主意)。
urls_returning_200 = []
for url in find_from_sublister(hostname):
url = "http://" + url.strip()
try:
response = requests.get(url)
if response.status_code == 200:
urls_returning_200.append(url)
print " \n {} {}\n".format(response.status_code, url)
except requests.exceptions.RequestException as e:
print "Can't make the request to this Subdomain {}\n".format(url)
print "finding the url's whose status code is 200"
print urls_returning_200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.