Sun*_*her 13 html python lxml literals python-3.x
我正在通过lxml.builder从列表构建HTML表,并努力在其中一个表格单元格中建立链接
列表以下列方式生成:
with open('some_file.html', 'r') as f:
table = etree.parse(f)
p_list = list()
rows = table.iter('div')
p_list.append([c.text for c in rows])
rows = table.xpath("body/table")[0].findall("tr")
for row in rows[2:]:
p_list.append([c.text for c in row.getchildren()])
Run Code Online (Sandbox Code Playgroud)
我解析的HTML文件与 lxml进一步生成的相同,即我为测试目的设置了某种递归.
以下是我如何构建表格
from lxml.builder import E
page = (
E.html(
E.head(
E.title("title")
),
E.body(
....
*[E.tr(
*[
E.td(E.a(E.img(src=str(col)))) if ind == 8 else
E.td(E.a(str(col), href=str(col))) if ind == 9 else
E.td(str(col)) for ind, col in enumerate(row)
]
) for row in p_list ]
Run Code Online (Sandbox Code Playgroud)
当我通过文字指定链接时,一切都很顺利.
E.td(E.a("link", href="url_address"))
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试输出列表元素值(即https://blahblahblah.com
)作为链接时
E.td(E.a(str(col), href=str(col)))
Run Code Online (Sandbox Code Playgroud)
细胞是空的,细胞中没有显示任何细胞.
如果我将链接文本指定为文字并放入str (col)
href,则链接显示正常,但它不是真正的href,而是包含生成的html文件的名称.
如果我只输出该col
值作为字符串
E.td(str(col))
Run Code Online (Sandbox Code Playgroud)
它显示正常,即它不是空的.有什么问题E.a
和E.img
元素?
只是注意到只有当我从html文件构建列表时才会发生这种情况.当我手动构建列表时,这样一切都输出正常.
p_list = []
p_element = ['id']
p_element.append('value')
p_element.append('value2')
p_list.append(p_element)
Run Code Online (Sandbox Code Playgroud)
电流输出(注意<a>
和<href>
标签)
<html>
<head>
<title>page</title>
</head>
<body>
<style type="text/css">
th {
background-color: DeepSkyBlue;
text-align: center;
vertical-align: bottom;
height: 150px;
padding-bottom: 3px;
padding-left: 5px;
padding-right: 5px;
}
.vertical {
text-align: center;
vertical-align: middle;
width: 20px;
margin: 0px;
padding: 0px;
padding-left: 3px;
padding-right: 3px;
padding-top: 10px;
white-space: nowrap;
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
}</style>
<h1>title</h1>
<p>This is another paragraph, with a</p>
<table border="2">
<tr>
<th>
<div class="vertical">ID</div>
</th>
...
<th>
<div class="vertical">I blacklisted him</div>
</th>
</tr>
<tr>
<td>1020</td>
<td>ТаисияСтрахолет</td>
<td>No</td>
<td>Female</td>
<td>None</td>
<td>Санкт-Петербург</td>
<td>Росiя</td>
<td>None</td>
<td>
<a>
<img src=" "/>
</a>
</td>
<td>
<a href=" ">
</a>
</td>
...
</tr>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
期望的输出
<html>
<head>
<title>page</title>
</head>
<body>
<style type="text/css">
th {
background-color: DeepSkyBlue;
text-align: center;
vertical-align: bottom;
height: 150px;
padding-bottom: 3px;
padding-left: 5px;
padding-right: 5px;
}
.vertical {
text-align: center;
vertical-align: middle;
width: 20px;
margin: 0px;
padding: 0px;
padding-left: 3px;
padding-right: 3px;
padding-top: 10px;
white-space: nowrap;
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
}</style>
<h1>title</h1>
<p>This is another paragraph, with a</p>
<table border="2">
<tr>
<th>
<div class="vertical">ID</div>
</th>
...
<th>
<div class="vertical">I blacklisted him</div>
</th>
</tr>
<tr>
<td>1019</td>
<td>МихаилПавлов</td>
<td>No</td>
<td>Male</td>
<td>None</td>
<td>Санкт-Петербург</td>
<td>Росiя</td>
<td>C.-Петербург</td>
<td>
<a>
<img src="http://i.imgur.com/rejChZW.jpg"/>
</a>
</td>
<td>
<a href="http://i.imgur.com/rejChZW.jpg">link</a>
</td>
...
</tr>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
得到它自己.问题不在于生成,而在于解析HTML.解析函数没有获取IMG
和A
嵌套的标签,TD
并且列表的这些元素是空的.由于程序的严格逻辑(从文件中获取+从站点API获取),我无法检测到问题的原因.
正确的解析逻辑应该是:
for row in rows[1:]:
data.append([
c.find("a").text if c.find("a") is not None else
c.find("img").attrib['src'] if c.find("img") is not None else
c.text
for c in row.getchildren()
])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
346 次 |
最近记录: |