python

python+phantomjs抓取js渲染后的网页内容

phantomjs官方公告,phantomjs已经停止维护了,所以这个方案基本可放弃了,改用selenium+chromedriver的方法更合适

phantomjs官方下载地址:https://phantomjs.org/download

在Python中的具体使用方法

phantomjs属于selenium库,所以使用方法很简单

from selenium import webdriver
driver=webdriver.PhantomJS(executable_path=r'D:/phantomjs目录/bin/phantomjs.exe')
driver.get('http://news.sohu.com/scroll/')
print(driver.find_element_by_class_name('title').text)

当调用PhantomJS时需要设置参数路径

如果执行报错:AttributeError: module 'selenium.webdriver' has no attribute 'PhantomJS'

报错翻译:模块“selenium”。“webdriver”没有属性“PhantomJS”

这个报错的意思是在selenium.webdriver模块中找不到有关PhantomJS的信息。

因为新版的selenium已经放弃了PhantomJS。目前的selenium版本已经更新到了4.15.2以上的版本了。

如果仍想要使用的话,建议降低selenium版本,下载2.48.0版本。

卸载pip uninstall selenium

重新安装指定版本pip install selenium==2.48.0


编辑:

阅读量:45

url链接:https://www.qozr.com/cms_python-phantomjs-zhua-qu-js-xuan-ran-hou-de-wang-ye-nei-rong.html

Tag标签: phantomjs , python , selenium

同类新闻

更多新闻

Copyright © 2007-2023 千欧中软 版权所有 https://www.qozr.com seo | 网站建设 [渝ICP备15005074号] 渝公网安备50011802011077