Roberto Lozano
Roberto Lozano

Reputation: 53

Xpath - Cant obtain information using Text() but i cant see the path and the number

Im working on a project, but i cant obtain one piece of information using Xpath.

https://www.vallen.com.mx/detalle/?des=PAL-09-ALT1120&articulo=Arnes-Altitude-Cuerpo-Completo-con-Ajuste-Tipo-Fricci%C3%B3n-Anillos-D-en-Espalda-y-Cintura

I look at the code and i was able to obtain prices, photos, and other information but not the inventory (Existencia, in spanish) i obtain only the label name so it retrives "Existencia:" text but not the amount.

I try //*[@id="valExistencia"]/text()[2] "retrieve blank" and without [2], bring data label but not inventory amount.

I would appreciate if anyone can help me. I cant obtain the data and i really need the information.

If i look at the code is as:

from lxml import html
import requests

#Importar de un TXT simple, un solo dato por renglon
filename= open("listado_urls.txt")
url = [urls.rstrip('\n') for urls in filename.readlines()]

#Hacer un loop
for urlunico in url:
    page = requests.get(urlunico)
    tree = html.fromstring(page.content)
    inventory = tree.xpath('//div[@class="row"]/div[@class="col-md-12"]/span[@id="valExistencia"]/text()[2]'

Upvotes: 2

Views: 54

Answers (1)

Andersson
Andersson

Reputation: 52675

Required data generated dynamically by JavaScript. You cannot get this data with requests. You might need to use, for example, selenium+PhantomJS instead:

from selenium import webdriver as web
from selenium.webdriver.support.ui import WebDriverWait as wait

driver = web.PhantomJS()
driver.get("https://www.vallen.com.mx/detalle/?des=PAL-09-ALT1120&articulo=Arnes-Altitude-Cuerpo-Completo-con-Ajuste-Tipo-Fricci%C3%B3n-Anillos-D-en-Espalda-y-Cintura")
existencia = driver.find_element_by_id("valExistencia")
wait(driver, 10).until(lambda x: existencia.text != 'Existencia:')
print(existencia.text)

This should allow you to get text from required span just after it has been changed (number was generated)

Upvotes: 2

Related Questions