【问题标题】:Change directory in python and extract .html filenames through scrapy spider在python中更改目录并通过scrapy spider提取.html文件名
【发布时间】:2012-01-19 06:23:23
【问题描述】:

我编写了一个爬虫,它会爬过一个名为 fid 的文件夹,并提取所有子文件夹的名称作为链接。现在的问题是这些子文件夹中的每一个都有一个 html 页面,我想提取所有这些 html 文件的名称并添加到当前的“start_urls”,这样我就可以从所有这些 html 中刮出所需的信息页。我试过了:

os.listdir()
glob.glob()

但这些都不起作用。请帮我解决这个问题。

【问题讨论】:

  • 您能提供一些代码作为起点吗?

标签: python scrapy


【解决方案1】:

一种标准库方法是将os.walkfnmatch 结合使用:

import fnmatch
import os

start_urls = []

for root, dirnames, filenames in os.walk('/start/dir/'):
    for filename in fnmatch.filter(filenames, '*.html'):
        start_urls.append(os.path.join(root, filename))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2013-06-22
    • 2014-03-18
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多