Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我跑得很慢_Python_Selenium - Fatal编程技术网

Python 我跑得很慢

Python 我跑得很慢,python,selenium,Python,Selenium,我正在尝试编写一个python脚本来检查百思买库存(如果可能的话,稍后再检查)。大约需要90秒才能完成。我不确定我使用selenium的方式是否有问题,或者我可以做些什么来提高性能。下面是日志。先谢谢你 from selenium import webdriver import sys, time def main(): url = sys.argv[1] sku_t = 'skuId=' sku = url[url.index(sku_t)+len(sku_t):]

我正在尝试编写一个python脚本来检查百思买库存(如果可能的话,稍后再检查)。大约需要90秒才能完成。我不确定我使用selenium的方式是否有问题,或者我可以做些什么来提高性能。下面是日志。先谢谢你

from selenium import webdriver
import sys, time

def main():
    url = sys.argv[1]
    sku_t = 'skuId='
    sku = url[url.index(sku_t)+len(sku_t):]

    chrome_options = webdriver.ChromeOptions()
    chrome_options.headless = True
    chrome_options.add_argument('--window-size=1920,1080')
    prefs = {"profile.managed_default_content_settings.images": 2}
    chrome_options.add_experimental_option("prefs", prefs)
    driver = webdriver.Chrome(options=chrome_options)
    start_time = time.time()
    driver.get(url)
    print("--- %s seconds ---" %(time.time() - start_time))

    css_param = 'button.add-to-cart-button[data-sku-id="'+sku+'"]'
    add_to_cart_btn = driver.find_element_by_css_selector(css_param)
    print(add_to_cart_btn.text)
    driver.quit()

if __name__ == '__main__':
    main()

有一些API是专门为您提供的。如果您不想使用API,请尝试包括这些chrome\u选项

DevTools listening on ws://127.0.0.1:62866/devtools/browser/fb0dfd00-0aee-4bec-9e6e-8411e18593d6
[0130/193519.329:INFO:CONSOLE(127)] "bby_privacy: getServerComponent", source: https://nexus.ensighten.com/bestbuy/privacy_init/Bootstrap.js (127)
[0130/193519.660:INFO:CONSOLE(143)] "getServerComponent suppressed", source: https://nexus.ensighten.com/bestbuy/privacy_prod/Bootstrap.js (143)
[0130/193529.699:INFO:CONSOLE(0)] "Access to script at 'https://assets.bbystatic.com/shop/master-ad/dist/client/client-978b7e491e0889e92a05356af57308d2.mjs' from origin 'https://www.bestbuy.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.", source: https://www.bestbuy.com/site/xfx-amd-radeon-rx-6800-16gb-gddr6-pci-express-4-0-gaming-graphics-card-black/6442077.p?skuId=6442077 (0)
[0130/193535.454:INFO:CONSOLE(1)] "Uncaught (in promise) t: request to /button-state/api/v5/button-state?skus=6442077&conditions=&storeId=&destinationZipCode=&context=pdp&consolidated=false&source=buttonView&xboxAllAccess=false timed out after 5000ms", source: https://www.bestbuy.com/~assets/bby/_com/fulfillment/fulfillment-summary/dist/client/client-51a75235b1a89815b2bb615b099f1639.js (1)
[0130/193539.609:INFO:CONSOLE(0)] "Access to script at 'https://assets.bbystatic.com/shop/display-ad/dist/client/client-72a4443f23ae7927233dfaa3239ddc89.mjs' from origin 'https://www.bestbuy.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.", source: https://www.bestbuy.com/site/xfx-amd-radeon-rx-6800-16gb-gddr6-pci-express-4-0-gaming-graphics-card-black/6442077.p?skuId=6442077 (0)
[0130/193539.791:INFO:CONSOLE(718)] "LibEmbeddedComponentRenderer: Embedded component queries can only contain strings, numbers, booleans or CsiVar objects with non-empty name strings.
The query param object key "locale" contains unsupported value:  undefined", source: https://www.bestbuy.com/~assets/bby/_com/gvp-287c65223d1c49e3d4181939b6c98ef4.min.js (718)
[0130/193540.756:INFO:CONSOLE(8)] "firing initData from parent", source: https://www.bestbuy.com/~assets/bby/_com/analytics-dotcom/chunks/tagIframe-6b64f0d171878bd514b6.js (8)
[0130/193540.758:INFO:CONSOLE(6)] "starting gate fired!", source: https://nexus.ensighten.com/bestbuy/tagframe_dev/code/d0f9a4f369cc4aace811197f11e3a9b9.js?conditionId0=1195706 (6)
[0130/193541.920:INFO:CONSOLE(14)] "canary version not set", source: https://www.bestbuy.com/~assets/bby/_com/@ds/unified-chat/dist/javascript/unified-chat-init-f92615d5.js (14)
[0130/193558.181:INFO:CONSOLE(1)] "Failed to pass the CSI variable to account libTypeError: Cannot read property 'innerText' of undefined", source: https://www.bestbuy.com/~assets/bby/_com/shop/header/dist/client/client-current-6040b166d0f69756a5b4d62f14255057.mjs (1)
[0130/193558.593:INFO:CONSOLE(14)] "Dispatching button is sold out for  - 233--null", source: https://assets.bbystatic.com/fulfillment/add-to-cart-button/dist/client/client-807bf7e58728fff64dda829e243a6d4e.js (14)
[0130/193558.627:INFO:CONSOLE(14)] "Dispatching button is sold out for  - 233-77001-null", source: https://assets.bbystatic.com/fulfillment/add-to-cart-button/dist/client/client-807bf7e58728fff64dda829e243a6d4e.js (14)
[0130/193603.621:INFO:CONSOLE(1)] "Uncaught (in promise) t: request to /fulfillment/ispu/api/ispu/v2 timed out after 5000ms", source: https://www.bestbuy.com/~assets/bby/_com/fulfillment/fulfillment-summary/dist/client/client-51a75235b1a89815b2bb615b099f1639.js (1)
[0130/193603.655:INFO:CONSOLE(1)] "Uncaught (in promise) t: request to /fulfillment/shipping/api/v1/fulfillment/sku;skuId=6442077;postalCode=77001;deliveryDateOption=EARLIEST_AVAILABLE_DATE timed out after 5000ms", source: https://www.bestbuy.com/~assets/bby/_com/fulfillment/fulfillment-summary/dist/client/client-51a75235b1a89815b2bb615b099f1639.js (1)
如果这些都不起作用,请尝试在没有headless模式的情况下运行脚本。 此外,您实际上不需要
selenium
来获取可用性和价格,因为它们存储在页面源代码中。因此,您也可以使用
requests
检查物品的可用性,并且仅在物品可用且您希望将其添加到购物车时调用
selenium

chrome_options.add_argument('--no-proxy-server')
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument("--proxy-bypass-list=*")
chrome_options.add_argument('--blink-settings=imagesEnabled=false')