python - Python——用beautifulsoup对“展开”按钮中的内容进行网页抓取
问题描述
我正在刮一张黄页,以获取城市中所有物理治疗师的姓名。通过 url,我得到了 50 名物理治疗师的列表,但是,当我展开页面时,url 并没有改变。如何获得完整的姓名列表?
这就是我获得罗斯托克市物理治疗师名单的方式。
url = 'https://www.gelbeseiten.de/Suche/Physiotherapie%20praxis/Rostock'
req = requests.get(url, headers= header)
soup = BeautifulSoup(req.content, 'html.parser')
names = []
business_name = soup.find_all('h2', attrs ={"data-wipe-name":"Titel"})
for name in business_name:
names.append(name.get_text())
在网址的底部有一个名为 的按钮Mehr Anzeigen
,基本上是说“显示更多”。如果我点击那里,物理治疗师的条目数量将从 50 变为 60。有 90 名物理治疗师的参赛作品。当我多次单击该按钮并显示所有条目时,该按钮消失了。这列出了城里所有的理疗师,我想要这个。
单击“显示更多”后如何获取所有条目?
解决方案
这个简单的任务不需要使用 Selenium。https://www.gelbeseiten.de/AjaxSuche
通过使用 Chrome 的开发人员工具,您可以观察到该网站在按下包含以下数据的“Mehr anzeigen”按钮时使用了一个简单的 POST 请求:
umkreis: -1
WAS: Physiotherapie praxis
WO: rostock
position: 51
anzahl: 10
sortierung: relevanz
json 响应包含一个html
包含所有搜索结果的键。此外,响应中还有gesamtanzahlTreffer
和键。不幸的是,通过设置和anzahlTreffer
,不可能通过单个 POST 请求获得所有搜索结果。但是,第一个 POST 请求包含前 50 个结果(类似于网站),通过每个新的 POST 请求,我们可以获得接下来的 10 个结果。position=0
anzahl=100
长话短说,您可以像这样解析所有结果:
def post_ajax_search(was: str, wo: str, pos: int):
req = requests.post("https://www.gelbeseiten.de/AjaxSuche", data={
'umkreis': -1, 'WAS': was, 'WO': wo, 'position': pos, 'sortierung': 'relevanz'})
r = req.json()
return [r[key] for key in ("gesamtanzahlTreffer", "html", "anzahlTreffer")]
def parse_html(html: str) -> list[str]:
soup = BeautifulSoup(html, "lxml")
return [i.text for i in soup.find_all("h2", {"data-wipe-name": "Titel"})]
def parser(was: str, wo: str) -> list[str]:
total_treffer, html, parsed_treffer = post_ajax_search(was, wo, 0)
all_items = parse_html(html)
i = 0
while parsed_treffer < total_treffer:
_, html, treffer = post_ajax_search(was, wo, 51 + i)
all_items += parse_html(html)
parsed_treffer += treffer
i += 10
return all_items
for praxis in (praxen := parser("Physiotherapie praxis", "rostock")):
print(praxis)
输出:
Göllner Sabine Krankengymnastik & Physiotherapie
Friemel Physiotherapie Inh. B. Neumann Krankengymnastik & Physiotherapie
Nehrenberg Dorothee Physiotherapie
Physiotherapiezentrum Marcel Frank
Silke Thiede Physiotherapie
Physiotherapie Kollmorgen
Buller Olaf Physiotherapie
Gemeinschaftspraxis Physiotherapie Möller & Norden
Physiotherapie Annekathrin Hinz
Physiotherapie Hinz Annekathrin Praxis für Physiotherapie
Physiotherapie K. Schuldt
Physiotherapie Richter Ralf-Uwe Physiotherapie
Sport-Physio Rostock, Inh. Tschiersch, Daniel Physiotherapie
Klimt Dagmar Physiotherapie
MedPrevio
Pause Andrea Physiotherapiepraxis
Sörgel Steffen
Doremans Monika Physiotherapie
Doremans Monika Physiotherapie
Friemel B. Physiotherapie
Physiotherapie Vital Speicher Katja Oestreich
Jürß Katherina Physiotherapie
Pietralczyk Regina Physiotherapie
Stoll Sven Physiotherapie
Tübbecke Carola Physiotherapie
Physiotherapie Reiser u. Behrens
Physiotherapeutische Praxis Rose
Arndt K. Physiotherapie
Arndt K. Physiotherapie
Hieke Gunnar Praxis für Physiotherapie
PTB Physiopraxis
PTB Physiopraxis
Physiotherapie Rhea Brüdigam
Duske Sandra
Achsnig Marion Physiotherapie
Berthold Physiopraxis
Bohn Katharina Praxis für Physiotherapie
Erdmann L. Physiotherapie
Hennig Heidlinde Physiotherapie
Klatt Gabriele Physiotherapie
Physio- & Hydrotherapie Evelyn Ruß-Deuschle
Physiometik-Physiotherapie und Kosmetik
PhysioPlus Martin Berthold
Physiotherapie Elke Wegener
Physiotherapie Inh. Doreen Bastian
Therapiewelten Fromm Inh. Andrea Fromm Physiotherapie
Therapiewelten Fromm Inh. Andrea Fromm Physiotherapie
Therapiewelten Fromm Inh. Andrea Fromm Physiotherapie
vital & physio GmbH Portwich, Rene & Kristina
Neumann Andre Physiotherapie
Physiotherapie Heike Braun u.Gisela Wessel-Schutz
Physiotherapie Monika Laasch
Physiotherapiepraxis Briese Inke u. Engel Katrin
Schawaller, Mertens Physiotherapie
Ahrens Ch. Hoffmann B. Kautz K. Wiechert M. Physiotherapiepraxis
Lenz Andrea Praxis für Physiotherapie
PhysioKiDa
Physiotherapie Birgit Paul
Physiotherapie Hirsch U.
Maaß Ingrid Physiotherapie
Physiotherapie Birgit Vogt
Müller Holger Physiotherapie
Physiotherapie A. Fischer-Pifrement
Physiotherapie Schuberth Simone
Skupin Anne, Praxis für Physiotherapie und Kinderphysiotherapie
Stoll Sven Physiotherapie
Physiotherapiepraxis Lasch
Physiotherapie Leyer
Simon Petra Physiotherapie
Erdmann Petra Physiotherapeutische Praxis
Doremans-Harms Monika Physiotherapie
Holz-Gräfe Ulrike Physiotherapie
Kannenberg u. Swensson Praxisgemeinschaft für Physiotherapie
Keßler Dirk Physiotherapie
Physiotherapie Ahrens Ch., Hoffmann B., Kautz K. u. Wiechert M.
Physiotherapie Dorit Schumacher Praxis für Physiotherapie
Physiotherapie Höhnerbach
Physiotherapie Kerstin Wikert Physiotherapeutin
Physiotherapie Kollmorgen
Physiotherapie Neumann
Physiotherapie Physikalische Therapie Inh. Karin Hellmuth
Physiotherapiepraxis Angela Keller
Pöschmann Kathleen Menschen"s"kinder Physiotherapie
PTB Physiopraxis
Roberto Kollmorgen
Rothkirch Physiotherapie Ramona
Schmidt Josephine Praxis für Physiotherapie
Stoll Sven Physiotherapie
Strauß Arne
Thoms Christiane Physiotherapie
推荐阅读
- python - Python - 从提供的模式生成文件名
- python - 将 Session 与 HTTPAdapter 一起使用时,Python 请求不会引发异常
- php - foreach 循环仅输出数组中的最后一个元素
- javascript - 如何使用开始禁用但必须开始启用的组件?
- python - 某些 DAG 运行后气流回填中断的可能原因
- php - 在 Laravel 中检查用户的角色
- python - 从亚马逊抓取数据以创建产品跟踪代码
- python - 使用自定义验证器进行 Django 文件验证无法按预期工作
- java - 用户输入字符串的问题(Java)
- typescript - 无法弄清楚如何在 Typescript 中结合回调和匿名函数