【问题标题】:Python dynamically load tuple/list from settings filePython从设置文件动态加载元组/列表
【发布时间】:2015-02-02 06:47:31
【问题描述】:

我想从设置文件中动态加载列表/元组。

我需要编写一个爬取网站的爬虫,但我想知道找到的文件,而不是页面。

我允许用户在settings.py 文件中指定此类文件类型,如下所示:

# Document Types during crawling
textFiles = ['.doc', '.docx', '.log', '.msg', '.pages', '.rtf', '.txt', '.wpd', '.wps']
dataFiles = ['.csv', '.dat', '.efx', '.gbr', '.key', '.pps', '.ppt', '.pptx', '.sdf', '.tax2010', '.vcf', '.xml']
audioFiles = ['.3g2','.3gp','.asf','.asx','.avi','.flv','.mov','.mp4','.mpg','.rm','.swf','.vob','.wmv']


#What lists would you like to use ?
fileLists = ['textFiles', 'dataFiles', 'audioFiles']

我将我的设置文件导入crawler.py

我使用beautifulsoup模块从HTML内容中查找链接,处理如下:

for item in soup.find_all("a"):
            # we dont want some of them because it is just a link to the current page or the startpage
            if item['href'] in dontWantList:
                continue

            #check if link is a file based on the fileLists from the settings
            urlpath = urlparse.urlparse(item['href']).path
            ext = os.path.splitext(urlpath)[1]
            file = False
            for list in settings.fileLists:
                if ext in settings.list:
                    file = True
                    #found file link
                    if self.verbose:
                        messenger("Found a file of type: %s" % ext, Colors.PURPLE)
                    if ext not in fileLinks:
                        fileLinks.append(item['href'])

            #Only add the link if it is not a file
            if file is not True:
                links.append(item['href'])
            else:
                #Do not add the file to the other lists
                continue

以下代码段抛出错误:

 for list in settings.fileLists:
                if ext in settings.list:

显然是因为 python 认为 settings.list 是一个列表。

有没有办法告诉 python 从设置文件中动态查找列表?

【问题讨论】:

  • 不要命名你自己的变量list,你会隐藏内置的。此外,使用set 可以提高成员资格测试的效率。
  • settings.list 来自哪里?
  • 谢谢。我也修改了我的命名。我的 IDE 对此也不是很高兴 :)

标签: python list dynamic nested-lists


【解决方案1】:

我认为您正在寻找的不是:

if ext in settings.list:

你需要

ext_list = getattr(settings, list)
if ext in ext_list:

编辑: 我同意 jonrsharpe 对 list 的看法,所以我在我的代码中重命名了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多