Search code examples
pythonweb-scrapingxpathlxml

XPath Python Error: The 'list' object has no attribute 'xpath'


I'm brand new to Python and web scraping and cannot figure out what is wrong with my code for the life of me. Is it because I'm scraping just one element and not a list? I've checked my XPaths so many times. Right now I'm just trying to scrape the parcel number.

My code:

html = requests.get("https://jeffersonmo-assessor.devnetwedge.com/parcel/view/01401903003039/2023")

doc = lxml.html.fromstring(html.content)

parcel_info = doc.xpath('//div[@class="panel-body collapse in egfjbbgcdd"]')

parcelNumber = parcel_info.xpath('./td[@class="col-xs-6"]/div[@class="inner-value"]/text_content()')

parcelNumber

I tried the code above and received the "AttibuteError: 'list' object has no attribute 'xpath' message.


Solution

  • When I pulled the page I did not find the egfjbbgcdd class anywhere. Here's a more robust approach. The .xpath() method returns a list, so you need to extract specific elements from the list before searching further.

    import requests
    import lxml.html
    import sys
    
    response = requests.get("https://jeffersonmo-assessor.devnetwedge.com/parcel/view/01401903003039/2023")
    
    with open("page.html", "wt") as file:
        file.write(response.text)
    
    tree = lxml.html.fromstring(response.text)
    
    try:
        overview = tree.xpath('//div[@id="overview-body"]')[0]
    except IndexError:
        print("Unable to locate overview.")
        sys.exit(1)
    
    try:
        number = overview.xpath('.//td[@class="col-xs-6"]/div[@class="inner-value"]')[0].text_content()
    except IndexError:
        print("Unable to locate parcel number.")
        sys.exit(1)
    
    print(number)
    

    Output:

    01-4.0-19.0-3-003-039