Python--在"expand"按钮中用美丽的汤进行网络抓取内容



我正在刮一个黄色页面,以获取一个城市中所有理疗师的姓名。有了这个网址,我得到了50名理疗师的名单,然而,当我扩展页面时,网址没有改变。如何获取完整的姓名列表?

这就是我得到罗斯托克市理疗师名单的方式。

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())

在url的底部有一个名为Mehr Anzeigen的按钮,基本上说";显示更多";。如果我点击那里,理疗师的条目数量从50-60个变化。参赛者有90名物理治疗师。当我多次单击该按钮,显示所有条目时,该按钮将消失。这列出了这个城市所有的理疗师,我想得到这个。

我如何在点击";显示更多";?

这个简单的任务不需要使用Selenium。通过使用Chrome的开发工具,您可以观察到,当按下包含以下数据的"Mehr anzigen"按钮时,网站使用了一个简单的对https://www.gelbeseiten.de/AjaxSuche的POST请求:

umkreis: -1
WAS: Physiotherapie praxis
WO: rostock
position: 51
anzahl: 10
sortierung: relevanz

json响应包含一个包含所有搜索结果的html键。此外,响应中还有gesamtanzahlTrefferanzahlTreffer密钥。不幸的是,通过设置position=0anzahl=100,不可能通过一个POST请求获得所有搜索结果。然而,第一个POST请求包含前50个结果(类似于网站(,通过每个新的POST请求,我们可以获得接下来的10个结果。

长话短说,你可以这样解析所有结果:

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
BeautifulSoup是一个HTML解析器。

如果你需要点击HTML页面上的按钮,可以使用一个利用真实浏览器的工具,比如selenium。

如果你不想了解Selenium,的破解解决方案是在点击Mehr Anzeigen后下载HTML,然后使用BeautifulSoup进行解析。以下是显示所有90个条目后的HTML粘贴:https://pastebin.pl/view/raw/277d9ea1

最新更新