3 回答

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

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

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']
- 3 回答
- 0 關(guān)注
- 283 瀏覽
添加回答
舉報(bào)