如何获得< ol>的第一行使用美丽的汤,使其成为JSON密钥,其余的是列表的值?
我试图为每组节目(Set1,set2,encore)从setList.fm而不仅仅是没有分离的歌曲列表来制作另一组键值对。我无法弄清楚的是如何访问陈述表演集的元素,然后在歌曲之后添加歌曲列表,直到它达到下一组为止。这是我要访问的HTML: html from setList fm
,我的JSON文件看起来像这样:
''
"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
I am trying to make another set of key-value pairs for each set of a show (set1, set2, encore) scraped from setlist.fm instead of just the list of songs without separation. What I cannot figure out is how to access the elements that state the set of the show and then append the list of songs after it until it hits the next set. Here is the html I am accessing:
html code from setlist fm
Currently, my JSON file looks like this:
'''`{
"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
},`
whereas I want it to look like this:
"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
},`
Here is the code I am using to generate the song list of the JSON but am not sure how to get the sets individually:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您,您想将歌曲“将”“分组”到他们的部分:
打印:
If I understand you correctly, you want to "group" songs to their sections:
Prints: