Skip to content Skip to sidebar Skip to footer

Xpath Lookup Via Lxml Starting From Root Rather Than Element

I want to do the same thing I do in beautiful soup, find_all elements and iterate through them to find some other_elements in each iterated elements. i.e.: soup = bs4.BeautifulSoup

Solution 1:

Don't use // as prefix in the follow-up queries, which explicitly asks the query to start from the root rather than your current element. Instead, use .// for relative queries:

for item in tree.xpath('//div[@class="v-card"]'):
    name = item.xpath('.//span[@itemprop="name"]/text()'
    address = item.xpath('.//p[@itemprop="address"]/text()')

Post a Comment for "Xpath Lookup Via Lxml Starting From Root Rather Than Element"