我正在try 为从集合列表中抓取的每一组节目(set1、set2、encore)创建另一组键值对.调频,而不仅仅是歌曲列表,没有分离.我无法理解的是,如何访问表示该节目集的元素,然后将歌曲列表附加到它之后,直到它到达下一集.以下是我正在访问的html:
目前,我的JSON文件如下所示:
`{0}'
"artist": "Sample Artist",
"day": 20,
"month": 1,
"songs": ["Song A","Song B","Song C"
],
"tour": "2000 U.S. Tour",
"venue": "Sample Venue, Atlanta, GA, USA",
"year": 2000
},`
而我希望它看起来像这样:
"artist": "Sample Artist",
"day": 20,
"month": 1,
"songs": ["Song A","Song B","Song C"
],
"set1": ["Song A"],
"set2": ["Song B"],
"encore":["Song C"],
"tour": "2000 U.S. Tour",
"venue": "Sample Venue, Atlanta, GA, USA",
"year": 2000
},`
下面是我用来生成JSON歌曲列表的代码,但我不确定如何单独获取这些歌曲集:
def getConcertData(i, url, concerts):
try:
soup = getSoup(url)
dateBlock = soup.find_all("div", {"class": "dateBlock"})[0]
infoContainer = soup.find_all("div", {"class": "infoContainer"})[0]
headLineDiv = infoContainer.find_all("div", {"class": "setlistHeadline"})[0]
setlistDiv = soup.find_all("div", {"class": "setlistList"})[0]
#removed unrelated code for question
songs = []
for a in setlistDiv.find_all("a", {"class": "songLabel"}):
songs.append(a.getText().strip())
print(str(year)+"."+str(month).zfill(2)+"."+str(day).zfill(2)+": "+venue)
data = dict()
data["artist"] = artist
data["year"] = year
data["month"] = month
data["day"] = day
data["venue"] = venue
data["tour"] = tour
data["songs"] = songs
# data["set1"] = 0
# data["set2"] = 0
# data["encore"] = 0
concerts[i] = data