Python JSON列表嵌套实现及爬虫数据按首字母分组问题
Python实现JSON列表嵌套结构及习语爬取分组方案
1. 如何在Python的JSON文件中实现列表嵌套结构?
要实现JSON的列表嵌套结构,核心是构造字典+列表的组合数据结构:
- 用字典的键作为分组标识(比如你的字母A/B/C)
- 对应的值设为列表,列表中存放每个分组下的具体数据(比如单个习语的字典)
最终序列化后的JSON会呈现{"分组标识": [数据1, 数据2, ...], ...}的嵌套格式。
2. 习语爬取按首字母分组的修改方案
你的代码目前是把所有习语存入同一个列表,未按字母分组。只需调整数据存储逻辑,即可实现需求:
修改后的完整代码
import requests from bs4 import BeautifulSoup import json headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'} url = "https://www.englishclub.com/ref/Idioms/" mylist = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W" ] # 初始化分组字典,键为字母,值为对应习语列表 grouped_data = {} for letter in mylist: # 为当前字母初始化空列表 grouped_data[letter] = [] result = requests.get(f"{url}{letter}/", headers=headers) doc = BeautifulSoup(result.text, "html.parser") for tag in doc.select('.linktitle a'): result = requests.get(tag['href']) doc_detail = BeautifulSoup(result.text, "html.parser") # 将习语数据添加到当前字母对应的列表中 grouped_data[letter].append({ 'idiom': doc_detail.h1.get_text(strip=True), 'meaning': doc_detail.select_one('h1 ~ h2 + p').get_text(strip=True), 'examples': [e.get_text(strip=True) for e in doc_detail.select('main ul li')] }) # 将分组后的字典写入JSON文件 with open('idioms.json', 'w', encoding='utf-8') as f: json.dump(grouped_data, f, ensure_ascii=False, indent=4)
关键修改点说明
- 替换原有的
data列表为grouped_data字典,用于按字母分组存储 - 遍历每个字母时,先为该字母在字典中创建空列表,后续爬取的习语直接追加到对应列表
- 细节优化:把内层的
doc变量改为doc_detail,避免覆盖外层的页面解析对象 - 用f-string拼接URL,代码更简洁易读
最终JSON结构示例
生成的JSON会是如下嵌套格式:
{ "A": [ { "idiom": "above board", "meaning": "If something is above board, it's been done in a legal and honest way.", "examples": [ "I'm sure the deal was completely above board as I know James well and he'd never do anything illegal or corrupt.", "The minister claimed all the appointments were above board and denied claims that some positions had been given to his friends." ] } ], "B": [ // B开头的习语列表... ] }
内容的提问来源于stack exchange,提问作者NewPartizal
相关产品推荐
相关产品推荐

