需要注意自己当前编程的环境,如果目录没有写完整,则会报错:
FileNotFoundError: [Errno 2] No such file or directory: '第二天/tiebatieba/2.0页.html'
例子
我需要将一份文件保存在第二天/tieba文件夹下,尽管我的编程文件在第二天下面,但是当前的环境路径为lv19 爬虫开发与实战/第二天,我需要将“第二天”这个路径也加上去
file_path = '第二天/tieba/'+ self.tieba_name + str(self.page/50) + '页.html'
# print(file_path)
# file_path = str(self.page/50+1) + '页.html'
# print('准备保存'+file_path)
with open(file_path ,'w',encoding='utf-8') as f:
print('正在下载第%s页' % file_path)
f.write(data)