Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 get child elements (lxml)

I am using lxml with html:

from lxml import html
import requests

How would I check if any of an element's children have the class = "nearby" my code (essentially):

url = "www.example.com"
Page = requests.get(url)
Tree = html.fromstring(Page.content)
resultList = Tree.xpath('//p[@class="result-info"]')
i=len(resultList)-1 #to go though the list backwards
while i>0:
    if (resultList[i].HasChildWithClass("nearby")):
        print('This result has a child with the class "nearby"')

How would I replace "HasChildWithClass()" to make it actually work?

Here's an example tree:

...
    <p class="result-info">
        <span class="result-meta">
            <span class="nearby">
                ... #this SHOULD print something
            </span>
        </span>
    </p>
    <p class="result-info">
        <span class="result-meta">
            <span class="FAR-AWAY">
                ... # this should NOT print anything
            </span>
        </span>
    </p>
...
like image 518
Gaberocksall Avatar asked Mar 06 '26 22:03

Gaberocksall


1 Answers

I tried to understand why you use lxml to find the element. However BeautifulSoup and re may be a better choice.

lxml = """
    <p class="result-info">
        <span class="result-meta">
            <span class="nearby">
                ... #this SHOULD print something
            </span>
        </span>
    </p>
    <p class="result-info">
        <span class="result-meta">
            <span class="FAR-AWAY">
                ... # this should NOT print anything
            </span>
        </span>
    </p>
    """

But i done what you want.

from lxml import html

Tree = html.fromstring(lxml)
resultList = Tree.xpath('//p[@class="result-info"]')
i = len(resultList) - 1 #to go though the list backwards
for result in resultList:
    for e in result.iter():
        if e.attrib.get("class") == "nearby":
            print(e.text)

Try to use bs4

from bs4 import BeautifulSoup


soup = BeautifulSoup(lxml,"lxml")
result = soup.find_all("span", class_="nearby")
print(result[0].text)
like image 116
KC. Avatar answered Mar 09 '26 12:03

KC.