保存HTML元素树,包括使用硒的CSS属性
我正在将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论