欢迎来博主个人博客做客?https://www.xbbdbb.top/
Python版本为2.7.16,IDE为Pycharm,报错代码如下:
# 文件写入
with open(filename, "w") as f:
f.write(html)
报错内容如下:
Traceback (most recent call last):
File "D:/Pycharm/WorkSpace/spider/Tieba.py", line 55, in <module>
tiebaSpider(fullurl, beginPage, endPage)
File "D:/Pycharm/WorkSpace/spider/Tieba.py", line 44, in tiebaSpider
writePage(html, filename)
File "D:/Pycharm/WorkSpace/spider/Tieba.py", line 26, in writePage
with open(filename, "w") as f:
IOError: [Errno 22] invalid mode ('w') or filename: '\xe7\xac\xac1\xe9\xa1\xb5.html'
将报错代码修改为如下:
# 文件写入
f = open(filename.decode('utf-8'), 'w')
f.write(html)
即可成功运行,因为Python中的字符串大概分为str和Unicode两种形式,其中str常用的编码类型为utf-8,gb2312,gbk等,而Python使用Unicode作为编码的基础类型,在open(filename, ‘w’)这个方法中,filename这个参数必须是Unicode编码的参数才可正常运行。
欢迎来博主个人博客做客?https://www.xbbdbb.top/