python,lxml检索列表中的所有元素

时间:2017-01-25 01:08:00

标签: python html web-scraping lxml

我试图从网站

获取列表中的所有元素

来自以下html代码段:

<ul>
    <li class="name"> James </li>
    <li> Male </li>
    <li> 5'8" </li>
</ul>

我当前的代码使用xpath并将名称存储在列表中。有没有办法将所有三个字段作为列表?

我的代码:

name = tree.xpath('//li[@class="name"]/text()')

2 个答案:

答案 0 :(得分:3)

import lxml.html as LH
tree = LH.parse('data')
print(tree.xpath('//li[../li[@class="name" and position()=1]]/text()'))

打印

[' James ', ' Male ', ' 5\'8" ']

XPath '//li[../li[@class="name" and position()=1]]/text()'表示

//li             # all li elements
[                # whose
..               # parent
/                # has a child 
li               # li element
  [              # whose
   @class="name" # class attribute equals "name"
   and           # and 
   position()=1] # which is the first child element
  ]               
  /text()        # return the text of those elements 

答案 1 :(得分:2)

from lxml import html

text = '''<ul>
    <li class="name"> James </li>
    <li> Male </li>
    <li> 5'8" </li>
</ul>
<ul>
    <li class="name"> James </li>
    <li> Male </li>
    <li> 5'8" </li>
</ul>
<ul>
    <li class="name"> James </li>
    <li> Male </li>
    <li> 5'8" </li>
</ul>'''

tree = html.fromstring(text)
for ul in tree.xpath('//ul[li[@class="name"]]'):  # loop through the ul tag, whose child tag contains class attribute and the value is 'name'
    print(ul.xpath("li/text()")) # get all the text in the li tag

出:

[' James ', ' Male ', ' 5\'8" ']
[' James ', ' Male ', ' 5\'8" ']
[' James ', ' Male ', ' 5\'8" ']