有没有办法在Python中使用PhantomJS?

发布于 2021-02-02 23:18:37

我想在Python中使用PhantomJS。我用谷歌搜索了这个问题,但是找不到合适的解决方案。

我发现os.popen() 可能是一个不错的选择。但是我无法通过一些争论。

使用subprocess.Popen()可能是目前合适的解决方案。我想知道是否有更好的解决方案。

有没有办法在Python中使用PhantomJS?

关注者
0
被浏览
153
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    在python中使用PhantomJS的最简单方法是通过Selenium。最简单的安装方法是

    • 安装NodeJS
    • 使用Node的包管理器安装phantomjs: npm -g install phantomjs-prebuilt
    • 安装硒(在你的virtualenv中,如果正在使用)

    安装后,你可以简单地使用phantom:

    from selenium import webdriver
    
    driver = webdriver.PhantomJS() # or add to your PATH
    driver.set_window_size(1024, 768) # optional
    driver.get('https://google.com/')
    driver.save_screenshot('screen.png') # save a screenshot to disk
    sbtn = driver.find_element_by_css_selector('button.gbqfba')
    sbtn.click()
    

    如果你的系统路径环境变量设置不正确,则需要将确切路径指定为的参数webdriver.PhantomJS()。替换为:

    driver = webdriver.PhantomJS() # or add to your PATH
    

    …具有以下内容:

    driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看