gpt4 book ai didi

python - 使用python从没有数据名称的json文件中提取url

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:46 25 4
gpt4 key购买 nike

我有 json 文件,其中包含 900 篇文章的元数据,我想从中提取 URL。我的文件是这样开始的

[
{
"title": "The histologic phenotypes of …",
"authors": [
{
"name": "JE Armes"
},
],
"publisher": "Wiley Online Library",
"article_url": "https://onlinelibrary.wiley.com/doi/abs/10.1002/(SICI)1097-0142(19981201)83:11%3C2335::AID-CNCR13%3E3.0.CO;2-N",
"cites": 261,
"use": true
},

{
"title": "Comparative epidemiology of pemphigus in ...",
"authors": [
{
"name": "S Bastuji-Garin"
},
{
"name": "R Souissi"
}
],
"year": 1995,
"publisher": "search.ebscohost.com",
"article_url": "http://search.ebscohost.com/login.aspx?direct=true&profile=ehost&scope=site&authtype=crawler&jrnl=0022202X&AN=12612836&h=B9CC58JNdE8SYy4M4RyVS%2FrPdlkoZF%2FM5hifWcv%2FwFvGxUCbEaBxwQghRKlK2vLtwY2WrNNl%2B3z%2BiQawA%2BocoA%3D%3D&crl=c",
"use": true
},
.........

我想使用 objectpath 检查文件以创建 json.tree 以提取 URL。这是我要执行的代码

  1.    import json
2. import objectpath
3. with open("Data_sample.json") as datafile: data = json.load(datafile)
4. jsonnn_tree = objectpath.Tree(data['name of data'])
5. result_tuple = tuple(jsonnn_tree.execute('$..article_url'))

但是在创建树的步骤 4 中,我必须插入我认为文件中没有的数据名称。我怎样才能替换这条线?

最佳答案

您可以使用列表理解获取所有文章网址。

import json

with open("Data_sample.json") as fh:
articles = json.load(fh)

article_urls = [article['article_url'] for article in articles]

关于python - 使用python从没有数据名称的json文件中提取url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796366/

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