1 回答
TA贡献1735条经验 获得超5个赞
使用.string属性获取<script>数据:
import json
from bs4 import BeautifulSoup
html_text = '''<script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","name":"Hamilton Medical Group - Dunkeld","url":"https://www.healthdirect.gov.au/australian-health-services/23000130/hamilton-medical-group-dunkeld/services/dunkeld-3294-sterling","contactPoint":{"@type":"ContactPoint","telephone":"03 5572 2422","email":"","website":"http://www.hamiltonmedicalgroup.net.au","fax":"03 5571 1606"},"address":{"@type":"PostalAddress","streetAddress":"14 Sterling Street","addressLocality":"DUNKELD","addressRegion":"VIC","postalCode":"3294","addressCountry":"AU"}}</script>'''
soup = BeautifulSoup(html_text, 'html.parser')
parsed_data = json.loads(soup.find('script',type='application/ld+json').string)
# print parsed data to screen:
print(json.dumps(parsed_data, indent=4))
印刷:
{
"@context": "http://schema.org",
"@type": "Organization",
"name": "Hamilton Medical Group - Dunkeld",
"url": "https://www.healthdirect.gov.au/australian-health-services/23000130/hamilton-medical-group-dunkeld/services/dunkeld-3294-sterling",
"contactPoint": {
"@type": "ContactPoint",
"telephone": "03 5572 2422",
"email": "",
"website": "http://www.hamiltonmedicalgroup.net.au",
"fax": "03 5571 1606"
},
"address": {
"@type": "PostalAddress",
"streetAddress": "14 Sterling Street",
"addressLocality": "DUNKELD",
"addressRegion": "VIC",
"postalCode": "3294",
"addressCountry": "AU"
}
}
添加回答
举报