lxml.builder 中作为 href 的字符串变量

String variable as href in lxml.builder

我正在通过 lxml.builder 从列表中构建 HTML table 并努力在其中一个中创建 link table 个细胞

列表生成方式如下:

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()])
我解析的

HTML 文件 与 lxml 进一步生成的 相同,即我为测试目的设置了某种递归。

下面是我的构建方式 table

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 ]

当我通过文字指定 link 时一切正常。

E.td(E.a("link", href="url_address"))

但是,当我尝试将列表元素值(https://blahblahblah.com)输出为 link

E.td(E.a(str(col), href=str(col)))

单元格是空的,单元格中什么也没有显示。

如果我将 link 文本指定为文字并将 str (col) 放入 href,则 link 会正常显示,但它包含 而不是真正的 href生成的 html 文件的名称.

如果我只输出那个 col 值作为字符串

E.td(str(col))

正常显示,即不为空E.aE.img 元素有什么问题?

刚刚注意到只有当我从 html 文件构建列表时才会发生这种情况。当我像这样手动构建列表时,一切都很好。

p_list = []
p_element = ['id']
p_element.append('value')
p_element.append('value2')
p_list.append(p_element)

当前输出(注意<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>&#1058;&#1072;&#1080;&#1089;&#1080;&#1103;&#1057;&#1090;&#1088;&#1072;&#1093;&#1086;&#1083;&#1077;&#1090;</td>
         <td>No</td>
         <td>Female</td>
         <td>None</td>
         <td>&#1057;&#1072;&#1085;&#1082;&#1090;-&#1055;&#1077;&#1090;&#1077;&#1088;&#1073;&#1091;&#1088;&#1075;</td>
         <td>&#1056;&#1086;&#1089;i&#1103;</td>
         <td>None</td>
         <td>
           <a>
             <img src="&#10;          "/>
           </a>
         </td>
         <td>
           <a href="&#10;          ">
           </a>
         </td>
        ...
       </tr>
     </table>
   </body>
 </html>

期望输出

<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>&#1052;&#1080;&#1093;&#1072;&#1080;&#1083;&#1055;&#1072;&#1074;&#1083;&#1086;&#1074;</td>
        <td>No</td>
        <td>Male</td>
        <td>None</td>
        <td>&#1057;&#1072;&#1085;&#1082;&#1090;-&#1055;&#1077;&#1090;&#1077;&#1088;&#1073;&#1091;&#1088;&#1075;</td>
        <td>&#1056;&#1086;&#1089;i&#1103;</td>
        <td>C.-&#1055;&#1077;&#1090;&#1077;&#1088;&#1073;&#1091;&#1088;&#1075;</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>

只是一个猜测,但看起来 'str()' 可能正在逃避它。尝试 E.td(E.a(col, href=col))

我自己知道了。问题不在于生成,而在于解析 HTML。解析函数未获取嵌套在 TD 中的 IMGA 标记,并且列表的这些元素为空。由于程序的严格逻辑(从文件中获取 + 从站点中获取 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()
                ])