PanczerTank
PanczerTank

Reputation: 1130

Python web-scraping. xpath returning a blank Array

I am using Python with requests and lxml to return an array of prices from a Craigslist search. When I run the code, a blank array is printed. I tried a few different xpaths and none worked.

Edit: I added the rest of the code including where the function is called.

from tkinter import *
import requests
from lxml import html

window = Tk()
window.title('Craigslist Apartment Finder')
window.resizable(width=False, height=True)

def getSearch():
    s1 = searchterm1.get()
    s2 = searchterm2.get()
    s3 = searchterm3.get()

    if s1 != "":
        url = "https://newyork.craigslist.org/search/brk/roo?sort=date&availabilityMode=0&query=" + s1
    if s2 != "":
        url = url + "+" + s2
    if s3 != "":
        url = url + "+" + s3
    if s1 != "" or s2 != "" or s3 != "":
        print(url)
    else:
        print("No search terms entered.")

    page = requests.get(url)
    print(page)
    tree = html.fromstring(page.content)

    price = tree.xpath('//span[class="result-price"]/text()')

    print("Prices:", price)

searchterm1 = Entry(window)
searchterm2 = Entry(window)
searchterm3 = Entry(window)

programname = Label(window, text="Apartment Finder")
runbutton = Button(window, text="Run", bg="green", fg="white",  width=10, command=getSearch)

displayurl = Label(window, text="url")
programname.grid(row=0, column=0, columnspan=2)
runbutton.grid(row=0, column=2)
searchterm1.grid(row=1, column=0)
searchterm2.grid(row=1, column=1)
searchterm3.grid(row=1, column=2)
displayurl.grid(row=2, column=0, columnspan=3)

window.mainloop()

Upvotes: 1

Views: 168

Answers (1)

宏杰李
宏杰李

Reputation: 12158

//span[@class="result-price"]

add @ to attribute

Upvotes: 2

Related Questions