user7466620
user7466620

Reputation: 21

python, lxml retrieving all elements in a list

I'm attempting to get all elements in a list from a website

From the following html snippet:

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

My current code takes uses the xpath of and stores the names in a list. Is there a way to get all three fields as a list?

My code:

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

Upvotes: 2

Views: 2857

Answers (2)

宏杰李
宏杰李

Reputation: 12178

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

out:

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

Upvotes: 3

unutbu
unutbu

Reputation: 880987

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

prints

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

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

//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 

Upvotes: 6

Related Questions