AttributeError:“NoneType”对象没有属性“close”

2024-01-29

我是 python js 新手,我正在尝试运行一个可用的项目github https://github.com/nava45/flipkart-scraper

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

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

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

代码是 主要.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当你到达finally块时。您可以通过添加显式检查来确认这一点:

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

As to why驱动程序创建将失败,最可能的解释是安装问题。您可以通过在 Python 环境中运行一个简单的示例来测试这一点:

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

如果这无法打开 Firefox 并导航到 Stack Overflow 的首页,请检查适用于您的操作系统的 Selenium 文档。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AttributeError:“NoneType”对象没有属性“close” 的相关文章

随机推荐