从字典中获取URL

时间:2019-03-29 11:41:55

标签: python dictionary

如何浏览字典“ mydict”的每个键,并获取其键值(在我的情况下为URL),然后每次在单独的浏览器会话中打开每个URL?

仅尝试自动使每次在新浏览器会话中打开每个URL的位置自动

mydict = {
    'set_one_urls': ['https://www.yahoo.com', 'https://www.google.com'],
    'set_two_urls': ['https://www.hotmail.com', 'https://www.godaddy.com']
}

for key in mydict():
    for value in key:
        driver = webdriver.Chrome(executable_path='../drivers/chromedriver.exe')
        driver.implicitly_wait(5)
        driver.maximize_window()
        driver.get(value)
        time.sleep(3)

2 个答案:

答案 0 :(得分:0)

您可以通过遍历mydict.items()来获取每组URL及其名称:

for set_name, url_set in mydict.items():
    for url in url_set:
        # do something with URL

答案 1 :(得分:0)

使用以下代码:

import time
import webbrowser

mydict = {
    'set_one_urls': ['https://www.yahoo.com', 'https://www.google.com'],
    'set_two_urls': ['https://www.hotmail.com', 'https://www.godaddy.com']
    }

for key, value in mydict.items():
    for link in value:
        webbrowser.open_new_tab(link)
        time.sleep(3)