我正在做一个项目,但我无法使用 Xpath 获取一条信息。
我查看代码,能够获取价格、照片和其他信息,但不能获取库存(Existencia,西类牙语),我只获取标签名称,因此它检索“Existencia:”文本,但不能检索金额。
我尝试//*[@id="valExistencia"]/text()[2]“检索空白”并且没有[2],带数据标签但不带库存数量。
如果有人能帮助我,我将不胜感激。我无法获取数据,但我确实需要这些信息。
如果我看看代码是这样的:
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]'
最佳答案
由 JavaScript 动态生成的所需数据
。您无法通过请求
获取此数据。例如,您可能需要使用 selenium
+ PhantomJS
相反:
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)
这应该允许您在更改后立即从所需的 span
中获取文本(生成数字)
关于python - Xpath - 无法使用 Text() 获取信息,但我看不到路径和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220061/