[Python] bs4.BeautifulSoup()调用本地html文件报错 FileNotFoundError: [Errno 2] No such file or directory: 'xxx.html'


问题描述:

Python 3.6 bs4.BeautifulSoup()调用本地已写好的html文件example.html, 报错找不到文件:

FileNotFoundError: [Errno 2] No such file or directory:

>>> exampleFile=open('example.html')
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    exampleFile=open('example.html')
FileNotFoundError: [Errno 2] No such file or directory: 'example.html'

解决思路:

此处example.html使用的是相对路径, 换成包含盘符的绝对路径后问题解决, 报错没有再出现.

>>> exampleFile=open('D:\DIV\Python Study1\CHAPTER11\example.html') >>> exampleSoup=bs4.BeautifulSoup(exampleFile)
>>> type(exampleSoup)
<class 'bs4.BeautifulSoup'>
>>> 

总结: 为避免错误, 调用本地文件时可使用绝对路径. 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM