从嵌套在另一个字典中的列表中的字典中拉出值。。。嵌套在另一本字典中

2024-05-29 04:59:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用谷歌CSE为一个项目做一些剪贴。这几乎是我第一次刮。几个季度前,我在学校上了一堂python课,刮削被定为我们最后的主题之一,但我们从未真正开始。无论如何

以下是我想做的:

使用谷歌CSE获取“观鸟”和“喂鸟”的谷歌新闻结果。从查询结果中,我想提取文章标题、文章链接以及文章发布日期。然后我想把这些都写进csv

以下是我到目前为止得到的信息(在https://gist.github.com/nikhilkumarsingh/5bce182ed57ae73f6cbde52fe846991b的大量帮助下,如果其他人正在寻找CSE的介绍,这本很好!!):

获取查询结果,使用for循环返回标题和链接。现在我只是把它打印出来,以确保得到结果。我稍后会写信给csv。我的queryresults对象是一个名为“result”的字典,它看起来像这样(很抱歉我要发布的代码太多了,但我的问题与嵌套有关,所以我认为这是最清楚的解释):

    {'kind': 'customsearch#search', 'url': {'type': 'application/json',
 'template': 'https://www.googleapis.com/customsearch/v1?q=
{searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe=
{safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr=
{cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq=
{hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter=
{siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms=
{excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite=
{relatedSite?}&dateRestrict={dateRestrict?}&lowRange=
{lowRange?}&highRange={highRange?}&searchType={searchType}&fileType=
{fileType?}&rights={rights?}&imgSize={imgSize?}&imgType=
{imgType?}&imgColorType={imgColorType?}&imgDominantColor=
{imgDominantColor?}&alt=json'}, 'queries': {'request': [{'title': 'Google 
Custom Search - bird watching', 'totalResults': '104000', 'searchTerms': 
'bird watching', 'count': 10, 'startIndex': 1, 'inputEncoding': 'utf8', 
'outputEncoding': 'utf8', 'safe': 'off', 'cx': 
'017465438656188383295:ul7lxhkonwq'}], 'nextPage': [{'title': 'Google 
Custom Search - bird watching', 'totalResults': '104000', 'searchTerms': 
'bird watching', 'count': 10, 'startIndex': 11, 'inputEncoding': 'utf8',
 'outputEncoding': 'utf8', 'safe': 'off', 'cx': 
'017465438656188383295:ul7lxhkonwq'}]}, 'context': {'title': 'google 
news'}, 'searchInformation': {'searchTime': 0.491713, 
'formattedSearchTime': '0.49', 'totalResults': '104000', 'formattedTotalResults': '104,000'}, 'items': [{'kind': 
'customsearch#result', 'title': 'Amy Cooper: White woman who called police 
on a black man in ...', 'htmlTitle': 'Amy Cooper: White woman who called 
police on a black man in ...', 'link': 
'https://news.google.com/articles/CAIiEDCQPCzyU2erjQLyLr_nLqUqGQgEKhAIACoH
CAowocv1CjCSptoCMPrTpgU?hl=en-US&gl=US&ceid=US%3Aen', 'displayLink': 
'news.google.com', 'snippet': 'May 26, 2020 ... White woman who called 
police on a black man bird-watching in Central Park \nhas been fired. By 
Amir Vera and Laura Ly, CNN. Updated 4:21\xa0...', 'htmlSnippet': 'May 26, 
2020 <b>...</b> White woman who called police on a black man <b>bird</b>-
<b>watching</b> in Central Park <br>\nhas been fired. By Amir Vera and 
Laura Ly, CNN. Updated 4:21&nbsp;...', 'formattedUrl': 
'https://news.google.com/.../CAIiEDCQPCzyU2erjQLyLr_ 
nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?...', 'htmlFormattedUrl': 
'https://news.google.com/.../CAIiEDCQPCzyU2erjQLyLr_ 
nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?...', 'pagemap': {'thumbnail': 
[{'src': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-
park-video-dog-video-african-american-trnd-screengrab-super-tease.jpg'}],
 'metatags': [{'template-top': 'us,news,art-vid-vls-col,col-top-news', 
'og:image': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-
central-park-video-dog-video-african-american-trnd-screengrab-super-
tease.jpg', 'twitter:card': 'summary_large_image', 'og:image:width': 
'1100', 'theme-color': '#000000', 'og:site_name': 'CNN', 'section': 'us', 
'vr:canonical': 'https://www.cnn.com/2020/05/26/us/central-park-video-dog-
video-african-american-trnd/index.html', 'article:content-tier': 'free', 
'og:description': 'The white woman who called police on a black man in 
Central Park during an encounter involving her unleashed dog has been 
fired from her job, her employer said Tuesday.', 'twitter:image': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 'og:pubdate': '2020-05-26T06:19:40Z', 'lastmod': '2020-05-26T20:21:18Z', 'pubdate': '2020-05-26T06:19:40Z', 'twitter:title': 'White woman who called police on a black man bird-watching in Central Park has been fired', 'og:type': 'article', 'thumbnail': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'author': 'Amir Vera and Laura Ly, CNN', 'og:title': 'White woman who 
called police on a black man bird-watching in Central Park has been 
fired', 'og:image:height': '619', 'fb:pages': '5550296508,18793419640', 
'referrer': 'unsafe-url', 'fb:app_id': '80401312489', 'viewport': 
'width=device-width, initial-scale=1.0, minimum-scale=1.0', 
'twitter:description': 'The white woman who called police on a black man 
in Central Park during an encounter involving her unleashed dog has been 
fired from her job, her employer said Tuesday.', 'og:url': 
'https://www.cnn.com/2020/05/26/us/central-park-video-dog-video-african-
american-trnd/index.html', 'article:opinion': 'false'}], 'cse_image': 
[{'src': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-
park-video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'width': '299', 'type': '1', 'height': '168'}], 'newsarticle': [{'image': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'keywords': 'us, Amy Cooper: White woman who called police on a black man 
in Central Park has been fired - CNN', 'author': 'Amir Vera and Laura Ly, 
CNN', 'ispartof': 'news', 'description': 'The white woman who called 
police on a black man in Central Park during an encounter involving her 
unleashed dog has been fired from her job, her employer said Tuesday.', 
'datecreated': '2020-05-26T06:19:40Z', 'url': 
'https://www.cnn.com/2020/05/26/us/central-park-video-dog-video-african-
american-trnd/index.html', 'articlebody': '(CNN)The white woman who called
 police on a black man in Central Park during an encounter involving her 
unleashed dog has been fired from her job, her employer said 
Tuesday."Following our internal...', 'datemodified': '2020-05-
26T20:21:18Z', 'articlesection': 'us', 'alternativeheadline': 'White woman who called police on a black man bird-watching in Central Park has been 
fired', 'headline': 'Amy Cooper: White woman who called police on a black 
man in Central Park has been fired - CNN', 'datepublished': '2020-05-
26T06:19:40Z', 'thumbnailurl': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg'}]}}

我拉出链接和标题的代码如下所示:

for item in result['items']:
    print(item['title'], item['link'])

以下是我一直坚持的观点:

文章发表日期的键“pubdate”嵌套在许多词典和列表中。我很难把它循环起来。嵌套,无论是以循环的形式还是以数据结构的形式,可能是我在编码方面最大的弱点

包含我感兴趣的所有信息的键是“items”,它的值是字典列表:


'items': [{'kind': 'customsearch#result', 'title': 'Amy Cooper: White 
woman who called police on a black man in ...', 'htmlTitle': 'Amy Cooper: 
White woman who called police on a black man in ...', 'link': 
'https://news.google.com/articles/CAIiEDCQPCzyU2erjQLyLr_nLqUqGQgEKhAIACoH
CAowocv1CjCSptoCMPrTpgU?hl=en-US&gl=US&ceid=US%3Aen', 'displayLink': 
'news.google.com', 'snippet': 'May 26, 2020 ... White woman who called 
police on a black man bird-watching in Central Park \nhas been fired. By 
Amir Vera and Laura Ly, CNN. Updated 4:21\xa0...', 'htmlSnippet': 'May 26,
 2020 <b>...</b> White woman who called police on a black man <b>bird</b>-
<b>watching</b> in Central Park <br>\nhas been fired. By Amir Vera and 
Laura Ly, CNN. Updated 4:21&nbsp;...', 'formattedUrl': 
'https://news.google.com/.../CAIiEDCQPCzyU2erjQLyLr_ 
nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?...', 'htmlFormattedUrl': 
'https://news.google.com/.../CAIiEDCQPCzyU2erjQLyLr_ 
nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?...', 'pagemap': {'thumbnail': 
[{'src': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-
park-video-dog-video-african-american-trnd-screengrab-super-tease.jpg'}], 
'metatags': [{'template-top': 'us,news,art-vid-vls-col,col-top-news', 
'og:image': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-
central-park-video-dog-video-african-american-trnd-screengrab-super-
tease.jpg', 'twitter:card': 'summary_large_image', 'og:image:width': 
'1100', 'theme-color': '#000000', 'og:site_name': 'CNN', 'section': 'us', 
'vr:canonical': 'https://www.cnn.com/2020/05/26/us/central-park-video-dog-
video-african-american-trnd/index.html', 'article:content-tier': 'free', 
'og:description': 'The white woman who called police on a black man in 
Central Park during an encounter involving her unleashed dog has been
 fired from her job, her employer said Tuesday.', 'twitter:image': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'og:pubdate': '2020-05-26T06:19:40Z', 'lastmod': '2020-05-26T20:21:18Z', 
'pubdate': '2020-05-26T06:19:40Z', 'twitter:title': 'White woman who 
called police on a black man bird-watching in Central Park has been 
fired', 'og:type': 'article', 'thumbnail': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'author': 'Amir Vera and Laura Ly, CNN', 'og:title': 'White woman who 
called police on a black man bird-watching in Central Park has been 
fired', 'og:image:height': '619', 'fb:pages': '5550296508,18793419640', 
'referrer': 'unsafe-url', 'fb:app_id': '80401312489', 'viewport': 
'width=device-width, initial-scale=1.0, minimum-scale=1.0', 
'twitter:description': 'The white woman who called police on a black man 
in Central Park during an encounter involving her unleashed dog has been 
fired from her job, her employer said Tuesday.', 'og:url': 
'https://www.cnn.com/2020/05/26/us/central-park-video-dog-video-african-
american-trnd/index.html', 'article:opinion': 'false'}]

在列表aka=result['items'][0]中的第一个字典中,我们有键“pagemap”,它的值是另一个字典,其中有键“metatags”,它的值是字典列表。此列表的第一个索引包含一个字典,其中的键的值是我正在查找的“pubdate”(我在代码块中放置了几个空格,以便您可以轻松找到此值):


'metatags': [{'template-top': 'us,news,art-vid-vls-col,col-top-news', 
'og:image': 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-
central-park-video-dog-video-african-american-trnd-screengrab-super-
tease.jpg', 'twitter:card': 'summary_large_image', 'og:image:width': 
'1100', 'theme-color': '#000000', 'og:site_name': 'CNN', 'section': 'us',
 'vr:canonical': 'https://www.cnn.com/2020/05/26/us/central-park-video-
dog-video-african-american-trnd/index.html', 'article:content-tier': 
'free', 'og:description': 'The white woman who called police on a black 
man in Central Park during an encounter involving her unleashed dog has 
been fired from her job, her employer said Tuesday.', 'twitter:image':
 'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'og:pubdate': '2020-05-26T06:19:40Z', 'lastmod': '2020-05-26T20:21:18Z',



'pubdate': '2020-05-26T06:19:40Z', 'twitter:title': 'White woman who 
called police on a black man bird-watching in Central Park has been 
fired', 'og:type': 'article', 'thumbnail': 
'https://cdn.cnn.com/cnnnext/dam/assets/200526102231-02-central-park-
video-dog-video-african-american-trnd-screengrab-super-tease.jpg', 
'author': 'Amir Vera and Laura Ly, CNN', 'og:title': 'White woman who 
called police on a black man bird-watching in Central Park has been 
fired', 'og:image:height': '619', 'fb:pages': '5550296508,18793419640', 
'referrer': 'unsafe-url', 'fb:app_id': '80401312489', 'viewport': 
'width=device-width, initial-scale=1.0, minimum-scale=1.0', 
'twitter:description': 'The white woman who called police on a black man 
in Central Park during an encounter involving her unleashed dog has been 
fired from her job, her employer said Tuesday.', 'og:url': 
'https://www.cnn.com/2020/05/26/us/central-park-video-dog-video-african-
american-trnd/index.html', 'article:opinion': 'false'}]


希望你能跟我一起穿过这个粗糙的鸟巢结构

因此,理想情况下,我要寻找的是一个能给我回报的循环:

Amy Cooper: White woman who called police on a black man in ... https://news.google.com/articles/CAIiEDCQPCzyU2erjQLyLr_nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?hl=en-US&gl=US&ceid=US%3Aen
2020-05-26T06:19:40Z

等等,我的查询结果中的下一个故事

我得到的最接近的结果是:

for item in result['items']:
        print(item['title'], item['link'])
        for date in result['items'][0]['pagemap']['metatags']:
            print (date['pubdate'])

这很接近,但仅返回第一个故事的日期,即使循环继续到下一个故事:

Amy Cooper: White woman who called police on a black man in ... https://news.google.com/articles/CAIiEDCQPCzyU2erjQLyLr_nLqUqGQgEKhAIACoHCAowocv1CjCSptoCMPrTpgU?hl=en-US&gl=US&ceid=US%3Aen
2020-05-26T06:19:40Z
Christian Cooper shouldn't need a Harvard degree to survive birding ... https://news.google.com/articles/CAIiEOCKmxd9S5s5cwM5xs0AivoqGAgEKg8IACoHCAowjtSUCjC30XQwzqe5AQ?hl=en-US&gl=US&ceid=US%3Aen
2020-05-26T06:19:40Z
People called police on this black birdwatcher so many times that he ... https://news.google.com/articles/CAIiEOkNNX95htD_KKDYihI5JcoqGAgEKg8IACoHCAowjtSUCjC30XQwzqe5AQ?hl=en-US&gl=US&ceid=US%3Aen
2020-05-26T06:19:40Z
A black man bird-watching in Central Park asked a white woman to ... https://news.google.com/articles/CAIiENZfU5G5gfmzo2CysHOaY0sqFQgEKg0IACoGCAowuLUIMNFnMLnhAg?hl=en-US&gl=US&ceid=US%3Aen
2020-05-26T06:19:40Z
What's a Tough Call in Bird Watching? Identifying a Gull - WSJ https://news.google.com/articles/CAIiEMKd4gQ1olRNd5T2Ndlpiu8qGAgEKg8IACoHCAow1tzJATDnyxUwuK20AQ
2020-05-26T06:19:40Z
Any advice, tips, help, or words of nested for loop wisdom would be greatly appreciated!!!!

Tags: inhttpscomonvideonewsogblack
1条回答
网友
1楼 · 发布于 2024-05-29 04:59:21

您每次都在result['items'][0]中访问数组的第一个单元格。工作代码:

for item in result['items']:
    print(item['title'], item['link'])
    for date in item['pagemap']['metatags']:
        print(date.get('pubdate', 'Pubdate is not specified'))

相关问题 更多 >

    热门问题