問題描述:
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'> >>>
總結: 為避免錯誤, 調用本地文件時可使用絕對路徑.
