我正在刮黄页,想知道一个城市里所有理疗师的名字。使用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())
在url的底部有一个名为Mehr Anzeigen
的按钮,基本上是说“显示更多”。如果我点击那里,理疗师的条目数量将从50-60个变化.有90名物理治疗师的参赛作品。当我多次单击该按钮,显示所有条目时,该按钮将消失。这是城里所有物理治疗师的名单,我想要这个。
如何在单击“显示更多”后获得所有条目?
发布于 2021-06-03 16:09:05
没有必要在这个简单的任务中使用Selenium。通过使用Chrome的开发工具,您可以观察到,当按下包含以下数据的“Mehr anzeigen”按钮时,该网站对https://www.gelbeseiten.de/AjaxSuche
使用简单的POST请求:
umkreis: -1
WAS: Physiotherapie praxis
WO: rostock
position: 51
anzahl: 10
sortierung: relevanz
json响应包含一个包含所有搜索结果的html
键。此外,响应中还有gesamtanzahlTreffer
和anzahlTreffer
键。不幸的是,通过设置position=0
和anzahl=100
无法通过一个POST请求获得所有搜索结果。然而,第一个帖子请求包含前50个结果(类似于网站),通过每一个新的帖子请求,我们可以获得接下来的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
发布于 2021-06-03 11:15:36
BeautifulSoup是一个HTML解析器。
如果需要单击HTML页面上的按钮,请使用使用实际浏览器的工具,如selenium。
如果您不想了解Selenium,一个hacky解决方案是在单击Mehr Anzeigen
之后下载HTML,然后使用BeautifulSoup解析它。下面是显示所有90个条目之后的HTML粘贴:https://pastebin.pl/view/raw/277d9ea1
https://stackoverflow.com/questions/67820093
复制相似问题