gpt4 book ai didi

python - 如何访问用于 python 中的 urllib 模块的字典值?

转载 作者:行者123 更新时间:2023-11-28 19:57:46 25 4
gpt4 key购买 nike

示例 - 我有以下字典...

URLDict = {'OTX2':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=OTX2&action=view_all',
'RAB3GAP':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=RAB3GAP1&action=view_all',
'SOX2':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=SOX2&action=view_all',
'STRA6':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=STRA6&action=view_all',
'MLYCD':'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=MLYCD&action=view_all'}

我想使用 urllib 在 for 循环中调用每个 url,如何实现?

我已经使用像这样的列表格式的 url 成功地做到了这一点......

OTX2 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=OTX2&action=view_all'
RAB3GAP = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=RAB3GAP1&action=view_all'
SOX2 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=SOX2&action=view_all'
STRA6 = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=STRA6&action=view_all'
MLYCD = 'http://lsdb.hgu.mrc.ac.uk/variants.php?select_db=MLYCD&action=view_all'

URLList = [OTX2,RAB3GAP,SOX2,STRA6,PAX6,MLYCD]

for URL in URLList:
sourcepage = urllib.urlopen(URL)
sourcetext = sourcepage.read()

但我还希望稍后在返回数据时能够打印 key 。使用列表格式,键将是一个变量,因此无法访问它进行打印,我只能打印值。

感谢您的帮助。

汤姆

最佳答案

你试过了吗(举个简单的例子):

for key, value in URLDict.iteritems():
print key, value

关于python - 如何访问用于 python 中的 urllib 模块的字典值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com