Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用lxml/xpath解析html元素_Python_Html_Xpath_Lxml - Fatal编程技术网

Python 使用lxml/xpath解析html元素

Python 使用lxml/xpath解析html元素,python,html,xpath,lxml,Python,Html,Xpath,Lxml,使用lxml/python和xpath,我检索了标记之间的值。 我想得到的html属性,不仅是文本,我的程序工作,但跳过了两行 python: #!/usr/bin/env python # -*- coding: utf-8 -*- import lxml.html htmltree = lxml.html.parse('data.html') res = htmltree.xpath("//table[@class='mainTable']/tr/td/text()") print '\n

使用lxml/python和xpath,我检索了标记之间的值。 我想得到的html属性,不仅是文本,我的程序工作,但跳过了两行

python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import lxml.html
htmltree = lxml.html.parse('data.html')
res = htmltree.xpath("//table[@class='mainTable']/tr/td/text()")
print '\n'.join(res).encode("latin-1")
data.html示例

<table class='mainTable'>
         <TR>
                  <TD bgcolor="#cccccc">235</TD>
                  <TD bgcolor="#cccccc"> Windows XP / Office 2003.</TD>
                  <TD bgcolor="#cccccc">
                  G:\REMI\projets\Migration_XP_Office2003\Procedures\Installation Win XP et Office 2003.doc</TD>
                  <TD bgcolor="#cccccc">2005-10-18</TD>
                  <TD bgcolor="#cccccc">2010-12-30</TD></TR>
                  <TD bgcolor="#cccccc">
                  <P class="MsoBodyText" 
                    style="margin: 0cm 0cm 0pt;"><STRONG><FONT face="Times New Roman" size="5">blablablablablablbala<BR><BR></FONT></STRONG></FONT></P>
                  </TD>
                <TR>
                  <TD bgcolor="#cccccc">23</TD>
                  <TD bgcolor="#cccccc">XEROX/ MAC</TD>
                  <TD bgcolor="#cccccc">
                    <P>joint.</P>
                    <P>&nbsp;</P></TD>
                  <TD bgcolor="#cccccc">G:\DDTH_INF\REMI\bdcfiles\I098_Page_de_garde_MAC.doc</TD>
                  <TD bgcolor="#cccccc">2012-12-19</TD>
                  <TD bgcolor="#cccccc">2012-12-19</TD>
         </TR>
 </table>
我不明白为什么程序跳过了

<P class="MsoBodyText" 
                        style="margin: 0cm 0cm 0pt;"><STRONG><FONT face="Times New Roman" size="5">blablablablablablbala<BR><BR></FONT></STRONG></FONT></P>

blablbala

接头。

因为它在
标记之间?我只想得到每个TD之间的所有数据。我也尝试过使用/tr/td/p/但它不是解决方案


注意:这段代码是一个示例,可能是html被破坏了,但我的文件结构良好

这是因为您从每个
td
元素中获取
text()
,这基本上意味着给我一个直接位于
td
元素内部的文本节点

相反,请调用找到的每个
td

texts = [td.text_content() for td in htmltree.xpath("//table[@class='mainTable']/tr/td")]

尝试
res=htmltree.xpath(“//table[@class='mainTable']/tr/td//text()”)
获取所有子文本节点。只需一句简单的注释:仅对包含正确html的html页面使用lxml。您可以在许多地方找到lxml无法处理的未关闭标记(事实上,只有在编写HTML时才使用它…)。如果不确定,使用BeautifulSoup with可以很好地修复此类(轻微)错误。非常感谢我的朋友,我更了解,我今天才开始使用xpath,我还是一个初学者,感谢text()方法的精确含义。
 <P>joint.</P>
                        <P>&nbsp;</P>
texts = [td.text_content() for td in htmltree.xpath("//table[@class='mainTable']/tr/td")]