首页 > 解决方案 > 如何使用 selenium 和 PhantomJS 从动态网站中提取值

问题描述

我正在尝试获取计时器的值 > http://prntscr.com/kcbwd8 在此网站上 > https://www.whenisthenextsteamsale.com/ 并希望将其存储在变量中。

import urllib
from bs4 import BeautifulSoup as bs
import time
import requests
from selenium import webdriver
from urllib.request import urlopen, Request
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3"}

browser = webdriver.PhantomJS()
browser.get('https://www.whenisthenextsteamsale.com/')

soup = bs(browser.page_source, "html.parser")
result = soup.find_all("p",{"id":"subTimer"})

for item in result:
    print(item.text)

browser.quit()

我尝试使用上面的代码,但它返回此错误>

C:\Users\rober\Anaconda3\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium 对 PhantomJS 的支持已被弃用,请使用 Chrome 或 Firefox 的无头版本代替
warnings.warn( '对 PhantomJS 的 Selenium 支持已被弃用,请使用无头' 19:59:11

有没有什么办法解决这一问题 ?如果没有,还有另一种方法可以获取站点的动态值并将它们存储在变量中。

谢谢你。

标签: javascriptseleniumselenium-webdriverweb-scrapingphantomjs

解决方案


PhantomJs 不再被维护。 https://groups.google.com/forum/m/#!topic/phantomjs/9aI5d-LDuNE

您应该使用无头 chrome / firefox。

您将不得不替换此代码:

browser = webdriver.PhantomJS()
browser.get('https://www.whenisthenextsteamsale.com/')

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("--headless")
browser= webdriver.Firefox(firefox_options=options, executable_path="Path to geckodriver.exe")
browser.get('https://www.whenisthenextsteamsale.com/');

在此处下载 GeckoDriver:下载 GeckoDriver


推荐阅读