[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