javascript - 如何使用 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
有没有什么办法解决这一问题 ?如果没有,还有另一种方法可以获取站点的动态值并将它们存储在变量中。
谢谢你。
解决方案
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
推荐阅读
- c++ - 使用 9 轴 IMU 和车轮旋转/里程表来预测汽车位置的卡尔曼拟合器
- python - DQN 模型 ValueError:使用序列设置数组元素
- javascript - Console.log 错误:反应表单“表单提交已取消,因为表单未连接”,即使它正在提交
- c# - 使用 FlatPickr 进行硒测试
- sql - 我无法在 PostgreSQL 上创建表
- r - 更改饼图中的背景颜色
- javascript - 计算数字数组的总和
- javascript - 使用 javascript 方法 .split( ) 对 URL 进行切片
- c - 使用列表进行 Shell 模拟
- javascript - 如何处理同一页面不同图表的点击事件