1 回答
TA贡献1817条经验 获得超14个赞
实际上,您所需的desired数据是通过JavaScript页面加载动态加载的,因此requests包将无法JavaScript动态渲染。但我已经能够找到script保存数据的标签,然后将其加载到string中。JSON dictJSON
在这里你可以解析任何你想要的:)。
import requests
from bs4 import BeautifulSoup
import json
r = requests.get("https://www.comparis.ch/gesundheit/arzt/pathologie")
soup = BeautifulSoup(r.content, 'html.parser')
script = soup.find("script", {'id': '__NEXT_DATA__'}).text
data = json.loads(script)
print(data.keys()) # JSON Dict
dumper = json.dumps(data, indent=4)
print(dumper) # to see it in human readble format
就像是:
for item in data['props']['pageProps']['doctorResults']['doctorModels']:
print(item['name'])
输出:
Mohamed Abdou
Dr. med. Heiner Adams
Dr. med. Franziska Aebersold
Prof. Dr. med. Adriano Aguzzi
Dr. med. Maria Ammann
Prosper Anani
Dr. med. Max Arnaboldi
Dr. med. Walter Arnold
Dr. med. Irena Baltisser
Dr. med. Fridolin Bannwart
Dr. med. Yara Banz
Dr. med. André Barghorn
Dr. Jessica Barizzi
Prof. Dr. med. Daniel Baumhoer
Audrey Baur Chaubert
Dr. med. Christian Georg Bayerl
Dr. med. Marc Beer
Dr. med. Sabina Berezowska
Dr. med. Steffen Bergelt
Dr. med. Barbara Elisabeth Berger-Denzler
- 1 回答
- 0 关注
- 138 浏览
添加回答
举报