-1

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.

2
  • 1
    doc.xpath() returns a list of matching elements (even if there is only one). So yes, parcel_info is a list. Commented Jun 25 at 1:28
  • Assuming your Xpath is indeed correct, you can probably access the first element of the list by replacing parcel_info.xpath... with parcel_info[0].xpath... - instead of trying to access .xpath on the parcel_info list, accessing it on the first element in the list parcel_info[0]
    – Grismar
    Commented Jun 25 at 1:35

1 Answer 1

0

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
1
  • 1
    Thank you so much, it worked! I see a line that says "<div class="panel-body collapse in egfjbbgcdd" id="overview-body" style="padding:0px;">, which highlights the entire collapsible section labelled "property information." I'm not sure why it didn't work but I will work off of your example instead. I really appreciate it! Commented Jun 25 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.