gpt4 book ai didi

python - 如何在 python 中的双花括号内抓取特定数据

转载 作者:行者123 更新时间:2023-11-28 22:17:41 25 4
gpt4 key购买 nike

我正在尝试从网站的双 curl { } 中的字符串中抓取特定数据。如何设法取出这些数据?以下是网站上双 curl 的片段:

<div class="swatch-data">
{"thumbnailImageUrl":"https://www.jbl.com.ph/dw/image/v2/AAUJ_PRD/on/demandware.static/-/Sites-masterCatalog_Harman/default/dw367304ef/JBL_Endurance-SPRINT_Product-Image_Black_Front-1605x1605px.jpg?sw=270&amp;sh=330&amp;sm=fit&amp;sfrm=png","productUrl":"https://www.jbl.com.ph/JBL+Endurance+SPRINT.html?cgid=in-ear-headphones&amp;dwvar_JBL%20Endurance%20SPRINT_color=Black-GLOBAL-","productSupportUrl":"","productID":"JBLENDURSPRINTBLK","orderable":false,"availability":{"message":"","status":"NOT_AVAILABLE"},"price":{"unitLabel":"each","priceType":"standard","salesPrice":"N/A"},"realprice":{"salesPrice":"N/A"},"badges":["new"],"buttonText":"Sold Out","showProdLimit":{"status":""},"CTAEnable":true,"commerceSiteFlag":false,"showPromoTimerFlag":false,"isProProd":false}
</div>

谢谢。

编辑:附言。我确实使用 BeautifulSoup4,只是我真的只是一个菜鸟,还没有接触过 JSON。

最佳答案

bs4 的例子

import bs4
import json

html = """
<div class="swatch-data">
{"thumbnailImageUrl":"https://www.jbl.com.ph/dw/image/v2/AAUJ_PRD/on/demandware.static/-/Sites-masterCatalog_Harman/default/dw367304ef/JBL_Endurance-SPRINT_Product-Image_Black_Front-1605x1605px.jpg?sw=270&amp;sh=330&amp;sm=fit&amp;sfrm=png","productUrl":"https://www.jbl.com.ph/JBL+Endurance+SPRINT.html?cgid=in-ear-headphones&amp;dwvar_JBL%20Endurance%20SPRINT_color=Black-GLOBAL-","productSupportUrl":"","productID":"JBLENDURSPRINTBLK","orderable":false,"availability":{"message":"","status":"NOT_AVAILABLE"},"price":{"unitLabel":"each","priceType":"standard","salesPrice":"N/A"},"realprice":{"salesPrice":"N/A"},"badges":["new"],"buttonText":"Sold Out","showProdLimit":{"status":""},"CTAEnable":true,"commerceSiteFlag":false,"showPromoTimerFlag":false,"isProProd":false}
</div>
"""

soup=bs4.BeautifulSoup(html,'lxml')
js_data = json.loads(soup.find('div').text)

# if you want productID just get it
print(js_data['productID'])

输出

JBLENDURSPRINTBLK

关于python - 如何在 python 中的双花括号内抓取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51115004/

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