[英]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.