启动时,我的Scrapy抓取工具出现问题。
我使用ConfigParser来设置一个小的config.ini来设置我每次启动抓取工具时创建的表名。这是一种废弃的基本方法,但我仍然使用scrapy和python
我得到了以下错误:
File "c:\python27\lib\ConfigParser.py", line 279, in options
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'SectionOne'
2016-02-04 15:10:57 [twisted] CRITICAL:
这是我的config.py:
import ConfigParser
import os
Config = ConfigParser.ConfigParser()
Config.read(os.getcwd() + '/config.ini')
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
这是我的config.ini
[SectionOne]
nom_table: Seche_cheveux
这是我的pipeline.py:
import sqlite3
from datetime import date, datetime
import os
from config import *
TableName = ConfigSectionMap("SectionOne")['nom_table']
print TableName
class sqlite3Pipeline(object):
def __init__(self):
#initialisation de la base et connexion
try:
#self.setupDBCon()
self.con = sqlite3.connect(os.getcwd() + '/db.sqlite')
self.cur = self.con.cursor()
self.table_name = TableName
self.createTables()
except sqlite3.Error as e:
raise e
def createTables(self):
self.createMgTable()
def closeDB(self):
self.con.close()
def __del__(self):
self.closeDB()
def createMgTable(self, table_name):
self.cur.execute("CREATE TABLE IF NOT EXISTS" + table_name + "(\
nom TEXT UNIQUE, \
url TEXT UNIQUE, \
prix TEXT, \
stock TEXT, \
revendeur TEXT, \
livraison TEXT, \
img TEXT UNIQUE, \
detail TEXT UNIQUE, \
bullet TEXT UNIQUE, \
created_at DATE \
)")
def process_item(self, item, spider):
self.storeInDb(item)
return item
def storeInDb(self,item):
etc....
etc...
请告诉我如何使用scrapy crawler处理configparser?并且如果可能的话告诉我我做错了什么
有关我单独启动每个文件的信息,包含的所有打印功能都可以正常工作。
答案 0 :(得分:1)
ConfigParser.read()
会无声地失败。当前工作目录(os.getcwd()
)可能会发生变化,导致其无法找到config.ini
。
如果您的config.ini
文件位于config.py
旁边,则可以改为使用此文件:
Config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))