python - How to get the innerHTML of all first child elements as a list or dataframe
问题描述
I would like to get the innerHTML
of all first child elements of a specific tag that the class name is "list-group" by using selenium or beautifulsoup
in python.
HTML code:
<div id="history_1" class="list-group">
<div>
<p>a</p>
</div>
<div>
<p>b</p>
</div>
<div>
<p>c</p>
</div>
<p>
d
</p>
</div>
<div>
....
</div>
<div id="history_2" class="list-group">
<div>
<p>e</p>
</div>
<div>
<p>f</p>
</div>
<div>
<p>g</p>
</div>
<p>
h
</p>
</div>
I want to get the result like below:
result[0] = "<div><p>a</p></div>
"
result[1] = "<div><p>b</p></div>
"
result[2] = "<div><p>c</p></div>
"
result[3] = "<p>d</p>
"
result[4] = "<div><p>e</p></div>
"
result[5] = "<div><p>f</p></div>
"
result[6] = "<div><p>g</p></div>
"
result[7] = "<p>h</p>
"
Any help is appreciated.
解决方案
指你想达到的,你想得到的outerHTML
,不是innerHTML
。
用于//*
查询所有节点并parent
满足您的需求,如下所示:
elements = driver.find_elements_by_xpath("//*[parent::*[@class='list-group']]")
for element in elements:
print(element.get_attribute('outerHTML'))
推荐阅读
- .net - .NET 客户端库是否可以配置为使用 CNAME 进行 Kerberos 身份验证?
- c# - 无法解析 http 数据包编码部分
- javascript - javascript中的var需要多少内存以及何时分配?
- sql-server - 如何在 SSIS 包内的脚本中引用用户变量
- asp.net-mvc - 在 Kendo Grid 中绑定复选框时出错
- typescript - 如何在 TypeScript 中使用可选链?
- python - Eric Matthes15.3.3随机游走,为什么RandowWalk类没有x_values属性
- c# - 如何在 Unity 中实例化对象?
- linq - 使用带有左连接和 AsExpandable() 的 LINQ 查询获取数据
- oracle - 如果 1 列为空,Oracle Join 不返回数据