Scrapy无法识别同一目录中的txt文件

2024-10-06 11:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试写入,然后将数据从Scrapy读取到.txt文件中。这是我的爬虫程序的parse()代码。你知道吗

def parse(self, response):
    lastpage = response.xpath("/ol[@class='pagination']/li[7]/a/text()").extract()
    with open('num_pages.txt', 'wb') as f:
        f.write(lastpage)

此代码不会抛出错误,但稍后尝试读取相同的文件时会抛出错误。你知道吗

num_pages = int(open('num_pages.txt', 'r').strip())

即使有明显的数字_页面.txt在与spider相同的目录中,运行crawler会显示错误消息

No such file or directory: 'num_pages.txt.'

此外,即使写操作没有抛出错误,num_页面.txt手动打开时为空。文件I/o是否有问题?谢谢!你知道吗


Tags: 文件数据代码程序txtparseresponse错误