【问题标题】:Python-- webscraping for the content in "expand" button with beautifulsoupPython——用beautifulsoup抓取“展开”按钮中的内容
【发布时间】:2021-08-21 11:54:42
【问题描述】:

我正在刮一张黄页,以获取一个城市中所有物理治疗师的姓名。通过 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 webDriver,您可以自动执行“按钮单击”操作并获取包含所有名称的页面,然后您可以像已经完成的那样删除数据
  • 我没有检查网站,但是当有一个 xhr 请求来获取下一个项目时会发生这种情况。您必须检查网络请求并找到正确的请求。然后你也许可以使用它。
  • @AnkushPandit 不,我不知道。你能把我链接到正确的页面吗?谢谢!!
  • 网上有很多资源可以学习,这里有一个简单的例子:geeksforgeeks.org/browser-automation-using-selenium。你必须先在你的 python 环境中下载 selenium,然后你还需要安装 selenium webDriver(无论你使用的是 Chrome 还是 Mozilla)。
  • 您可以从预览中调用URL,它显示200,但手动显示错误顺便说一句,您可以在xhr中找到它

标签: python beautifulsoup yellow-pages


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    BeautifulSoup 是一个 HTML 解析器。

    如果您需要点击 HTML 页面上的按钮,请使用使用真实浏览器的工具,例如 selenium。

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

    【讨论】:

    • 我想为多个城市执行此操作。多次单击“显示更多”按钮后,我不想手动下载许多城市的 html 页面。一些大城市有超过1000个条目!
    • 哦,那么使用 Selenium 是自动化按钮点击的正确方法。
    猜你喜欢
    • 2020-10-04
    • 2019-06-28
    • 2015-01-05
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多