使用 selenium xpath 提取 html 源代码 [英] Extract html source code with selenium xpath

查看:24
本文介绍了使用 selenium xpath 提取 html 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个很长的表中提取数据.对于表中的每一行,我想复制一个特定的 HTML 代码.

我的 HTML 源代码(我非常想提取)如下所示:

<div id="stats9526577" class="dark"></div><img src="/detaljer-1.gif" onmouseover="view_stats(9526577, 14, 13, 4, 7, 10, 8, 6, 3);">

我尝试了 python 代码:

data = driver.find_elements_by_xpath('//div[@class="relative"]')

如何使用 xpath 在 python 中打印上述 HTML 源代码?

解决方案

要打印与 xpath 匹配的元素的 html,可以使用 get_attribute()innerHTML 作为参数,即:

data = driver.find_elements_by_xpath('//div[@class="relative"]')对于数据中的 el:html = el.get_attribute('innerHTML')打印(html)

I want to extract data from a table that is very long. For every row in the table I want to copy a specific HTML code.

My HTML source code (which I fully want to extract) looks like:

<div class="relative">
                  <div id="stats9526577" class="dark"></div>
                  <img src="/detaljer-1.gif" onmouseover="view_stats(9526577, 14, 13, 4, 7, 10, 8, 6, 3);">
                </div> 

I tried the python code:

data = driver.find_elements_by_xpath('//div[@class="relative"]')

How can I print the above HTML source code in python using xpath?

解决方案

To print the html of the elements matching the xpath, you can use get_attribute() with innerHTML as argument, i.e.:

data = driver.find_elements_by_xpath('//div[@class="relative"]')
for el in data:
    html = el.get_attribute('innerHTML')
    print(html)

这篇关于使用 selenium xpath 提取 html 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆