第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 beautiful soup 從腳本標(biāo)簽中提取 json?

如何使用 beautiful soup 從腳本標(biāo)簽中提取 json?

慕田峪7331174 2023-12-25 15:55:10
reviewCount我想使用 beautiful soup 從腳本標(biāo)簽中提取。嘗試了不同的方法但沒有成功。<script type="application/json" data-initial-state="review-filter">{"languages":[{"isoCode":"all","displayName":"Toutes les langues","reviewCount":"573"},{"isoCode":"fr","displayName":"fran?ais","reviewCount":"567"},{"isoCode":"en","displayName":"English","reviewCount":"6"}],"selectedLanguages":["all"],"selectedStars":null,"selectedLocationId":null}</script>
查看完整描述

3 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

這應(yīng)該可行,我絕對(duì)確定有一種更優(yōu)雅的方法:


import json

from bs4 import BeautifulSoup


html = '''

<script type="application/json" data-initial-state="review-filter">

{"languages":[{"isoCode":"all","displayName":"Toutes les langues","reviewCount":"573"},{"isoCode":"fr","displayName":"fran?ais","reviewCount":"567"},{"isoCode":"en","displayName":"English","reviewCount":"6"}],"selectedLanguages":["all"],"selectedStars":null,"selectedLocationId":null}

</script>

'''


soup = BeautifulSoup(html, 'html.parser')

res = soup.find('script')

json_object = json.loads(res.contents[0])


for language in json_object['languages']:

    print('{}: {}'.format(language['displayName'], language['reviewCount']))

輸出:


Toutes les langues: 573

fran?ais: 567

English: 6


查看完整回答
反對(duì) 回復(fù) 2023-12-25
?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

導(dǎo)入 json 并加載數(shù)據(jù)json,然后 iterarte 獲取所有reviewCount.


import json

html='''<script type="application/json" data-initial-state="review-filter">

{"languages":[{"isoCode":"all","displayName":"Toutes les langues","reviewCount":"573"},{"isoCode":"fr","displayName":"fran?ais","reviewCount":"567"},{"isoCode":"en","displayName":"English","reviewCount":"6"}],"selectedLanguages":["all"],"selectedStars":null,"selectedLocationId":null}

</script>'''


soup=BeautifulSoup(html,"html.parser")

item=soup.select_one('script[data-initial-state="review-filter"]').text

jsondata=json.loads(item)

for item in jsondata['languages']:

    print(item['reviewCount'])

輸出:


573

567

6


查看完整回答
反對(duì) 回復(fù) 2023-12-25
?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

import re


html = '''<script type="application/json" data-initial-state="review-filter">

{"languages":[{"isoCode":"all","displayName":"Toutes les langues","reviewCount":"573"},{"isoCode":"fr","displayName":"fran?ais","reviewCount":"567"},{"isoCode":"en","displayName":"English","reviewCount":"6"}],"selectedLanguages":["all"],"selectedStars":null,"selectedLocationId":null}

</script>'''



match = [item.group(1) for item in re.finditer('reviewCount":"(.+?)"', html)]


print(match)

輸出:


['573', '567', '6']


查看完整回答
反對(duì) 回復(fù) 2023-12-25
  • 3 回答
  • 0 關(guān)注
  • 283 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)