简体   繁体   English

如何在python的字典中的键中存储多个值?

[英]How to store multiple values in keys in dictionary in python?

Maybe my question title is different from the question content. 也许我的问题标题与问题内容不同。

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"

I am using the code to do some following stuffs. 我正在使用代码来做以下一些工作。

  1. It will find the subdomains from the Sub-lister (Locally) 它将从子列表列表中找到子域(本地)
  2. Then it will go to find the status code of each subdomain which was find by the sublister, with the help of for loop . 然后它将在for loop的帮助下找到由子列表器找到的每个子域的状态码。 for x in find_from_sublister(hostname):

Note: find_from_sublister(hostname) is a function for finding the subdomain from the sublister. 注意:find_from_sublister(hostname)是用于从子列表查找子域的功能。

  1. Then print the status code with the URL. 然后打印带有URL的状态码。 Here print " \\n " + str(req1) 在这里print " \\n " + str(req1)

[All goes well, but the problem starts here ] [一切顺利,但问题从这里开始]

Now what I want is to seperate the URLs which have 200 status code. 现在我想要的是分隔具有200个状态代码的URL。

so I heard then it can happen by using the dictionary in python. 所以我听说那可以通过在python中使用字典来实现。 So, I try to use the dictionary. 因此,我尝试使用字典。 As you can see dict = {req2 : req3} 如您所见, dict = {req2 : req3}

Now I also make a list of index which have value 200 现在我也列出了值200的索引

Then I compare the keys to list of index. 然后,我将键与索引列表进行比较。 here keys == statuscode[0] 这里的keys == statuscode[0]

and if they match then it should print all the URL's which have 200 status code. 如果它们匹配,则应打印所有状态代码为200的网址。

But the result I am getting is below, 但是我得到的结果低于

finding the url's whose status code is 200 
po 

You see the value po its else statment value, 您会看到值po的else陈述值,

else:
   print "po"

Now the problem is Why I am getting this else value why not the URL's which have status code 200? 现在的问题是,为什么我要得到这个else值,为什么状态码为200的URL不能呢?

Hope I explained you clearly. 希望我能清楚地向你解释。 And waiting for someone who talk to me on this. 等着有人跟我说话。

Thanks 谢谢

Note: I am Using Python 2.7 ver. 注意:我正在使用Python 2.7版本。

In your case, you don't even need the dictionary. 就您而言,您甚至不需要字典。

I've tried to clean up the code as much as possible, including more descriptive variable names (also, it's a bad idea to override builtin names like dict ). 我试图尽可能地清理代码,包括更多描述性的变量名(同样,重写诸如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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM