保存HTML元素树,包括使用硒的CSS属性

发布于 2025-02-08 12:29:55 字数 431 浏览 0 评论 0原文

我正在将python与硒一起使用。

我正在尝试进行一些网络刮擦。我有一个我想保存到离线文件的webelement(包含子元素)。到目前为止,我设法使用webElement.get_attribute('innerhtml')获得了我的WebElement的RAW HTML。这有效,但是,最终产品中没有CSS,因为网站上使用了样式表。因此,我想将这些CSS属性转换为内联。

我发现 this stackoverflow解决方案,该解决方案显示了如何获得元素的CSS属性。但是,获取这些数据,然后将HTML作为字符串解析,以在HTML标签的样式属性中添加这些属性,然后为所有子元素进行此操作,感觉就像是一项重要的工作。

因此,我想知道是否有更直接的方法。

I'm using Python with Selenium.

I am attempting to do some web scraping. I have a WebElement (which contains child elements) that I would like to save to a offline file. So far, I have managed to get the raw HTML for my WebElement using WebElement.get_attribute('innerHTML'). This works, but, no CSS is present in the final product because a stylesheet is used on the website. So I'd like to get these CSS properties converted to inline.

I found this stackoverflow solution which shows how to get the CSS properties of an element. However, getting this data, then parsing the HTML as a string to add these properties inside the HTML tag's style attribute, then doing this for all the child elements, feels like it'd be a significant undertaking.

So, I was wondering whether there was a more straightforward way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文