wangqianchongya 2021-12-25 18:39 采纳率: 70%
浏览 773
已结题

selenium和PhantomJS的配合使用:AttributeError: module 'selenium.webdriver' has no attribute 'PhantomJS'


from selenium import webdriver
import time
import csv
import pymongo

client = pymongo.MongoClient('localhost', 27017)
mydb = client['mydb']
qq_shuo = mydb['qq_shuo']

driver = webdriver.PhantomJS()
driver.maximize_window()

def get_info(qq):
    driver.get('http://user.qzone.qq.com/{}/311'.format(qq))
    driver.implicitly_wait(10)
    try:
        driver.find_element_by_id('login_div')
        a = True
    except:
        a = False
    if a == True:
        driver.switch_to.frame('login_frame')
        driver.find_element_by_id('switcher_plogin').click()
        driver.find_element_by_id('u').clear()
        driver.find_element_by_id('u').send_keys('账号')
        driver.find_element_by_id('p').clear()
        driver.find_element_by_id('p').send_keys('密码')
        driver.find_element_by_id('login_button').click()
        time.sleep(3)
    driver.implicitly_wait(3)
    try:
        driver.find_element_by_id('QM_OwnerInfo_Icon')
        b = True
    except:
        b = False
    if b == True:
        driver.switch_to.frame('app_canvas_frame')
        contents = driver.find_elements_by_css_selector('.content')
        times = driver.find_elements_by_css_selector('.c_tx.c_tx3.goDetail')
        for content, tim in zip(contents, times):
            data = {
                'time': tim.text,
                'content': content.text
            }
            qq_shuo.insert_one(data)

if __name__ == '__main__':
    qq_lists = []
    fp = open('C:/Users/LP/Desktop/QQmail.csv')
    reader = csv.DictReader(fp)
    for row in reader:
        qq_lists.append(row['电子邮件'].split('@')[0])
    fp.close()
    for item in qq_lists:
        get_info(item)

运行结果及报错内容


"D:\Program Files (x86)\Anaconda3\python.exe" E:/python/从零开始学Python网络爬虫_源代码/qq_kongjian.py
Traceback (most recent call last):
  File "E:/python/从零开始学Python网络爬虫_源代码/qq_kongjian.py", line 10, in <module>
    driver = webdriver.PhantomJS(executable_path=r'D:\Program Files (x86)\phantomjs-2.1.1-windows\bin\phantomjs.exe')
AttributeError: module 'selenium.webdriver' has no attribute 'PhantomJS'

Process finished with exit code 1

我的解答思路和尝试过的方法


driver = webdriver.PhantomJS(executable_path=r'D:\Program Files (x86)\phantomjs-2.1.1-windows\bin\phantomjs.exe')

img

img

img

img

  • 写回答

4条回答 默认 最新

  • 陈年椰子 2021-12-27 09:25
    关注

    如果一定要用 PhantomJS ,那要考虑卸载当前 Selenium , 降档 到3.141.0 或者以下版本去用

    如果只是为了无界面,也可以考虑用 chorme , 用无界面模式。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月25日

悬赏问题

  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号