<ul id="parameter2" class="p-parameter-list">
<li title='养生堂天然维生素E软胶囊'>商品名称:养生堂天然维生素E软胶囊</li>
<li title='720135'>商品编号:720135</li>
<li title='养生堂'>品牌:<a>养生堂</a></li>
</ul>
以上是网页源码,现通过xpath匹配所有Li
import lxml.etree as etree
html = html.decode("utf-8")
tree = etree.HTML(html)
property_list_reg = "//ul[@id='parameter2']/li/text()"
property_lst = tree.xpath(property_list_reg)
print len(property_lst)
输出结果:
3
商品名称:养生堂天然维生素E软胶囊
商品编号:720135
品牌:
如果换成:
property_list_reg = "//ul[@id='parameter2']/li//text()" #此处//
输出结果:
4
商品名称:养生堂天然维生素E软胶囊
商品编号:720135
品牌:
养生堂
显然,这不是我想要的结果,我想要的结果是这样
3
商品名称:养生堂天然维生素E软胶囊
商品编号:720135
品牌: 养生堂
求大神指导~ 该如何实现?
The correct way is to use Xpath’s
string
functionIn the definition of XPath, "//" is "/descendant-or-self::node()/", indicating the matching of the current node or descendant node, so you use "//text()" to confirm It will match the child nodes below. A clearer way is to match the li layer of the parent node and then manually process the child nodes.
Output
Product name: Yangshengtang Natural Vitamin E Soft Capsule
Product number: 720135
Brand: Yangshengtang
3