首页 > 解决方案 > 使用 BS4 + Python 从动态网站导出数据:

问题描述

我想将以下网站中的所有商店数据导出到 excel 文件中: https ://www.ybpn.de/ihre-parfuemerien

问题:地图是“动态的”,因此当您输入邮政编码时会加载所需的数据。

需要的数据存储在 div-class "storefinder__list-item" 中,在 data-"storefinder-reference" div-class 中具有唯一引用,例如:data-storefinder-reference="132"

我试过了:

soup.find("div", {"data-storefinder-reference": "132"})

但输出是:

我认为这个问题是由于页面是动态的,因此当您输入邮政编码时会加载所需的数据。因此,当我搜索参考 ID“132”时,它的“那里”,但没有加载到网站上,并且 bs4 找不到这个 ID。

有什么想法可以改进代码吗?

标签: pythonbeautifulsoup

解决方案


为此,您可能需要研究selenium和/或“firefox-headless”等工具。

尤其是 selenium 允许您使用 Python “远程控制”网页

这是一个教程:https ://realpython.com/modern-web-automation-with-python-and-selenium/


推荐阅读