AttributeError:'NoneType'对象没有属性'close' [英] AttributeError: 'NoneType' object has no attribute 'close'

查看:980
本文介绍了AttributeError:'NoneType'对象没有属性'close'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python js的新手,我正在尝试运行一个 github 上可用的项目.

I am new to python js I am trying to run a project that are available on github

但是当我尝试运行它时,出现以下错误

but when I am trying to run it gives following error

Traceback (most recent call last):   File "main.py", line 81, in
 <module>
     crawler_machine()   File "main.py", line 76, in crawler_machine
     driver.close()  AttributeError: 'NoneType' object has no attribute 'close' 
Exception AttributeError: "'Service' object has no attribute
 'process'" in <bound method Service.__del__ of
 <selenium.webdriver.phantomjs.service.Service object at 0x107d05790>>
 ignored

我遵循该项目中给出的所有说明

I follow all the instruction that are giving in that project

,代码为 main.py

and the code is main.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from config import URL_FORMAT, SCROLL_TIMES, PHANTOM_JS_PATH, WEBDRIVER
from parser import FParser
from optparse import OptionParser

import signal
import sys
import time

driver = None


def close(signal, frame):
    '''
    When you press Ctrl-C the browser closes
    '''
    global driver
    print('You pressed Ctrl+C!')
    driver.close()
    signal.pause()
    sys.exit(0)


def scroll_down(driver):
    '''
    This helps you to scroll the search results page to load more results
    '''
    for i in range(SCROLL_TIMES):
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(0.3)


def get_page(driver, url):
    driver.get(url)
    return driver.page_source


def press_the_button_2_crawl(driver, keyword):
    """
    Main function handles everything 
    """
    html_data = get_page(driver, URL_FORMAT % keyword)
    scroll_down(driver)
    parser = FParser(html_data)
    parser.store() #store into db
    time.sleep(2)
def crawler_machine(search_word=None):

        global driver

        optparser = OptionParser()
        optparser.add_option("-s", "--search",
                        type="string", dest="search")
        (options, args) = optparser.parse_args()
        keyword = options.search or search_word
        print "Keyword",keyword
        try:
            try:
                #headless phantomjs for 32bit unix based machines
                driver =  webdriver.PhantomJS(executable_path=PHANTOM_JS_PATH)

            except:
                #firefox
                driver = webdriver.Firefox()


            signal.signal(signal.SIGINT, close)
            press_the_button_2_crawl(driver, keyword)

        finally:
             driver.close()   #line 76=================================



if __name__ == '__main__':

    crawler_machine()  #line 81=================================

推荐答案

我的猜测是创建驱动程序的两个尝试-PhantomJS和FireFox-均失败,这意味着当您到达最终位置时,driver仍为None.堵塞.您可以通过添加显式检查来确认这一点:

My guess would be that both attempts to create a driver - PhantomJS and FireFox - fail, meaning that driver is still None when you reach the finally block. You could confirm this by adding an explicit check:

    finally:
        if driver:
            driver.close()
        else:
            print "Could not create driver"

关于为什么驱动程序创建失败的原因,最可能的解释是安装问题.您可以通过在Python环境中运行一个简单的示例来对此进行测试:

As to why driver creation would be failing, the most likely explanation is an installation problem. You can test this by running a trivial example in your Python environment:

>>> from selenium.webdriver import Firefox
>>> d = Firefox()
>>> d.get('http://stackoverflow.com')

如果这不能打开Firefox并导航到Stack Overflow的首页,请查看您操作系统的Selenium文档.

If this doesn't open Firefox and navigate to the front page of Stack Overflow, check the Selenium documentation for your OS.

这篇关于AttributeError:'NoneType'对象没有属性'close'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆