python3使用xpath解析html部分

python3使用xpath解析html部分,python,html,xpath,Python,Html,Xpath,我只想用路径xpath从下面的html中提取div-part-html-part。 我只是给出了这个链接html的一部分: <html> <head> <meta charset="utf-8"> <title>Items 1 to 20 -- Example Page 1</title> <script type="text/javascript"> var _gaq =

我只想用路径xpath从下面的html中提取div-part-html-part。 我只是给出了这个链接html的一部分:

    <html>
    <head>
    <meta charset="utf-8">
    <title>Items 1 to 20 -- Example Page 1</title>
    <script type="text/javascript">
     var _gaq = _gaq || [];
    _ gaq.push(['_setAccount', 'UA-23648880-1']);
   _gaq.push(['_trackPageview']);
   _gaq.push(['_setDomainName', 'econpy.org']);
   </script>
   </head>
   <body>
  <div align="center">1, <a    
       href="http://econpy.pythonanywhere.com/ex/002.html">[<font    
   color="green">2</font>]</a>, <a    
   href="http://econpy.pythonanywhere.com/ex/003.html">[<font 
   color="green">3</font>]</a>, <a   
   href="http://econpy.pythonanywhere.com/ex/004.html">[<font 
   color="green">4</font>]</a>, <a 
  href="http://econpy.pythonanywhere.com/ex/005.html">[<font 
 color="green">5</font>]</a></div>
#I just want to get this part html.
<div class ="item-body" 
 <div title="item1">
 <div title="buyer-name">Carson Busses</div>
 <span class="item-price">$29.95</span><br>
</div>
</div  
.......
<div title="buyer-info">
<div title="buyer-name">Earl E. Byrd</div>
<span class="item-price">$8.37</span><br>
</div>
<div title="buyer-info">
<div title="buyer-name">Patty Cakes</div>
<span class="item-price">$15.26</span><br>
</div>
<div title="buyer-info">
<div title="buyer-name">Derri Anne Connecticut</div>
<span class="item-price">$19.25</span><br>
</div>
<div title="buyer-info">
<div title="buyer-name">Moe Dess</div>
<span class="item-price">$19.25</span><br>
</div>
<div title="buyer-info">
<div title="buyer-name">Leda Doggslife</div>
<span class="item-price">$13.99</span><br>
</div>
.........
.........
<div title="buyer-info">
<div title="buyer-name">Rose Tattoo</div>
<span class="item-price">$114.07</span><br>
</div>
<div title="buyer-info">
<div title="buyer-name">Moe Tell</div>
<span class="item-price">$10.09</span><br>
</div>
<script type="text/javascript">  (function() {
var ga = document.createElement('script');     ga.type =     
'text/javascript'; ga.async = true;
ga.src = ('https:'   == document.location.protocol ? 'https://ssl'     
: 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; 
s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>

我希望它得到html列表,但结果是[],请帮助我,请使用xpath而不是其他方法。谢谢

我已经解决了这个问题,只需使用以下代码,在我的代码中添加//*即可:

 buy_info2 = tree.xpath('//div[contains(@title, "item-body")]//*')
然后我可以得到我想要的所有html元素。
谢谢

我已经解决了这个问题,只需使用以下代码,在我的代码中添加//*即可:

 buy_info2 = tree.xpath('//div[contains(@title, "item-body")]//*')
然后我可以得到我想要的所有html元素。
谢谢

您可以使用类名拉取div:

In [2]: from lxml import html

In [3]: xml = html.fromstring(h)

In [4]: div = xml.xpath("//div[@class='item-body']")[0]

In [5]: print(html.tostring(div))
<div class="item-body" title="item1">
 <div title="buyer-name">Carson Busses</div>
 <span class="item-price">$29.95</span><br>
</div>
[2]中的
:从lxml导入html
在[3]中:xml=html.fromstring(h)
[4]中的div=xml.xpath(“//div[@class='item-body']]”[0]
在[5]中:打印(html.tostring(div))
卡森巴士
29.95美元

您可以使用类名拉取div:

In [2]: from lxml import html

In [3]: xml = html.fromstring(h)

In [4]: div = xml.xpath("//div[@class='item-body']")[0]

In [5]: print(html.tostring(div))
<div class="item-body" title="item1">
 <div title="buyer-name">Carson Busses</div>
 <span class="item-price">$29.95</span><br>
</div>
[2]中的
:从lxml导入html
在[3]中:xml=html.fromstring(h)
[4]中的div=xml.xpath(“//div[@class='item-body']]”[0]
在[5]中:打印(html.tostring(div))
卡森巴士
29.95美元

可能是您的解决方案在这里尝试此方法。我只想使用xpath方法可能是您的解决方案在这里尝试此方法。我只想使用xpath方法谢谢您的回答。谢谢您的回答。