Python 如何遍历JSON列表来查找我网站的URL?

Python 如何遍历JSON列表来查找我网站的URL?,python,json,wordpress,Python,Json,Wordpress,我正在从我的网站中提取JSON数据,我想将我所有帖子的URL(永久链接)附加到一个列表中。我试图使用下面的代码遍历JSON文件,但我得到一个TypeError:当我尝试for循环时,“list”对象不可调用。有人能帮忙吗 导入urllib.request 导入json 获取\u数据\u url=”http://www.financialgenomeproject.net/wp-json/wp/v2/posts" json_get_data_url=urllib.request.urlopen(g

我正在从我的网站中提取JSON数据,我想将我所有帖子的URL(永久链接)附加到一个列表中。我试图使用下面的代码遍历JSON文件,但我得到一个TypeError:当我尝试for循环时,“list”对象不可调用。有人能帮忙吗

导入urllib.request
导入json
获取\u数据\u url=”http://www.financialgenomeproject.net/wp-json/wp/v2/posts"
json_get_data_url=urllib.request.urlopen(get_data_url)
resp=json.load(json\u get\u data\u url)
url_list=[]
对于resp中的i(“内容”):
如果我('rendered')='href=':
url_list.append(['href='])
打印(url\u列表)

加载的json是存储在变量
resp
中的列表。您不能调用列表,
resp('content')
正试图这样做。相反,只需迭代列表:

分别适用于i:
如果我['rendered']='href=':
url_list.append(['href='])
并不是说我也用
I['rendered']
替换了错误的
I('rendered')


老实说,循环中的代码没有多大意义。您应该阅读更多有关Python工作原理的信息。

您加载的json是存储在变量
resp
中的列表。您不能调用列表,
resp('content')
正试图这样做。相反,只需迭代列表:

分别适用于i:
如果我['rendered']='href=':
url_list.append(['href='])
并不是说我也用
I['rendered']
替换了错误的
I('rendered')


老实说,循环中的代码没有多大意义。您应该阅读更多有关Python工作原理的信息。

您只需要rendered键,因为protected是bool类型。然后您可以使用
str.index
选择要选择的字符串的哪一部分

import urllib.request
import json

get_data_url = "http://www.financialgenomeproject.net/wp-json/wp/v2/posts"
json_get_data_url = urllib.request.urlopen(get_data_url)
resp = json.load(json_get_data_url)


输出:

http://financialgenomeproject.net/2019/05/31/chapter-25-home-not-asset/
http://financialgenomeproject.net/2017/02/26/chapter-3-benjamin-franklin-first-tax-planner/
http://financialgenomeproject.net/table-of-contents/
http://financialgenomeproject.net/2018/11/30/chapter-22-land-ownership/
http://financialgenomeproject.net/about/
http://financialgenomeproject.net/2017/08/23/financial-genome-project-chapter-7/
http://financialgenomeproject.net/2017/09/24/financial-genome-project-chapter-8/
http://financialgenomeproject.net/2017/09/24/financial-genome-project-chapter-8/
http://financialgenomeproject.net/2016/12/09/financial-genome-project-1/
http://financialgenomeproject.net/2018/04/09/chapter-15-do-you-need-a-budget/
获取url所需的时间 现在使用打印

%%timeit
url_list=[]
for my_dict in resp:
    for key in my_dict['content']:
        if key == 'rendered':
            my_dict2=my_dict['content'][key]
            idi=my_dict2.index('href="http')+len('href="')
            idf=my_dict2.index('/"')+1
            url=my_dict2[idi:idf]
            url_list.append(url)
            print(url)
#1.42 ms ± 60.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


如您所见,如果包含
print(url)
,则时间会显著增加,因此,如果您不需要立即查看该行,则建议对此行进行注释

您只需要rendered键,因为protected是bool类型。然后您可以使用
str.index
选择要选择的字符串的哪一部分

import urllib.request
import json

get_data_url = "http://www.financialgenomeproject.net/wp-json/wp/v2/posts"
json_get_data_url = urllib.request.urlopen(get_data_url)
resp = json.load(json_get_data_url)


输出:

http://financialgenomeproject.net/2019/05/31/chapter-25-home-not-asset/
http://financialgenomeproject.net/2017/02/26/chapter-3-benjamin-franklin-first-tax-planner/
http://financialgenomeproject.net/table-of-contents/
http://financialgenomeproject.net/2018/11/30/chapter-22-land-ownership/
http://financialgenomeproject.net/about/
http://financialgenomeproject.net/2017/08/23/financial-genome-project-chapter-7/
http://financialgenomeproject.net/2017/09/24/financial-genome-project-chapter-8/
http://financialgenomeproject.net/2017/09/24/financial-genome-project-chapter-8/
http://financialgenomeproject.net/2016/12/09/financial-genome-project-1/
http://financialgenomeproject.net/2018/04/09/chapter-15-do-you-need-a-budget/
获取url所需的时间 现在使用打印

%%timeit
url_list=[]
for my_dict in resp:
    for key in my_dict['content']:
        if key == 'rendered':
            my_dict2=my_dict['content'][key]
            idi=my_dict2.index('href="http')+len('href="')
            idf=my_dict2.index('/"')+1
            url=my_dict2[idi:idf]
            url_list.append(url)
            print(url)
#1.42 ms ± 60.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


如您所见,如果包含
print(url)
,则时间会显著增加,因此,如果您不需要立即查看该行,则建议在该行上发表评论

这太完美了!非常感谢你。这段代码比我的体重级别稍微高一点,所以我要坐下来好好学习。再次感谢,别担心。我已经包括了这项任务的时间安排,有无
print
。如果你想,你可以浏览:)这太完美了!非常感谢你。这段代码比我的体重级别稍微高一点,所以我要坐下来好好学习。再次感谢,别担心。我已经包括了这项任务的时间安排,有无
print
。如果需要,您可以浏览它:)