1
我使用Python與請求和lxml從Craigslist搜索返回價格數組。當我運行代碼時,會打印一個空白數組。我嘗試了幾個不同的xpath,但都沒有工作。Python網頁抓取。 xpath返回一個空的數組
編輯:我添加了其餘的代碼,包括函數被調用的地方。
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()
你能否提供你正在運行的整個代碼,或測試案例?現在我看到的只是一個沒有被調用的函數。 – galfisher
發表示例url –
新增所有代碼@galfisher – PanczerTank