简体   繁体   English

从具有 Selenium python 的变量中提取 JavaScript 值时出错

[英]Error when extracting a JavaScript value from a variable with Selenium python

I'm attempting to get the extract the value of the variable remainingTimeString on this website in python using Selenium webdriver.我正在尝试使用 Selenium webdriver 在 python 中获取该网站上变量remainingTimeString时间字符串的值。 I'm trying to use the driver.execute_script() function.我正在尝试使用driver.execute_script() function。 Here is my code:这是我的代码:

import selenium.webdriver

options = selenium.webdriver.FirefoxOptions()
options.add_argument("--headless")

driver = selenium.webdriver.Firefox(options=options)

driver.get('https://shopgoodwill.com/item/151632327')
print(driver.execute_script("return remainingTimeString"))

However, when I run it, I get:但是,当我运行它时,我得到:

selenium.common.exceptions.JavascriptException: Message: ReferenceError: remainingTimeString is not defined

What should I do?我应该怎么办? the variable is clearly in a script when I check the HTML source.当我检查 HTML 源时,该变量显然在脚本中。 Thanks!谢谢!

That data is being pulled dynamically from an API, after page loads, so your options are - either use a WebDriverWait for that element (or an implicit wait for that matter), or use a less complex solution, like below (without selenium), where you inspect the Network Tab in Dev Tools, locate the API where data is being pulled from, and scrape that API directly:该数据是在页面加载后从 API 动态提取的,因此您的选择是 - 为该元素使用 WebDriverWait(或隐式等待),或使用不太复杂的解决方案,如下所示(无硒),在开发工具中检查网络选项卡的位置,找到从中提取数据的 API,然后直接刮取 API:

import requests
import pandas as pd


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}

url = 'https://buyerapi.shopgoodwill.com/api/ItemDetail/GetItemDetailModelByItemId/151632327'

r = requests.get(url, headers=headers)
df = pd.json_normalize(r.json())
print(df['remainingTime'][0])

Result printed in terminal:终端打印的结果:

'17h 56m '

The code above is extracting remainingTime only from that dataframe.上面的代码仅从 dataframe 中提取剩余时间。 There are detailed product infos in that json, so you can get other data as well, if you need. json 中有详细的产品信息,如果需要,您也可以获取其他数据。 Python requests documentation can be found at https://requests.readthedocs.io/en/latest/ Python 请求文档可以在https://requests.readthedocs.io/en/latest/找到

Also pandas documentation: https://pandas.pydata.org/pandas-docs/stable/index.html还有 pandas 文档: https://pandas.pydata.org/pandas-docs/stable/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM