arleigh418 / python-and-Taiwan-stock-market

Python 金融市場賺大錢聖經:寫出你的專屬指標
54 stars 17 forks source link

爬蟲遇到的問題 #29

Open erichuang888 opened 2 years ago

erichuang888 commented 2 years ago

您好: 之前按照書本在爬蟲抓取股價資料時,發現會出現下列文字 /Users/mac/Documents/spyder/yahoo_price.py:23: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 23 of the file /Users/mac/Documents/spyder/yahoo_price.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.

soup = BeautifulSoup(data.text) Traceback (most recent call last): 請問是什麼問題呢?我是使用mac同時是搭配spyder編譯器

arleigh418 commented 2 years ago

Hi 您好, 這屬於Warnings.其實Warnings並不是錯誤,只是一種提醒或警告. 應該是因為我的套件版本比您的舊,所以我沒有跳出這個警告.

例如它字面告您的,他建議您在BeautifulSoup()方法中,指定解析器為lxml. 遇到WARNINGS通常有三種選項:

  1. 看到WARNINGS選擇無視不在意,若您的套件版本不要進行升級等,並確定程式確實是可以正常運行,其實可以無視.
  2. 依照他的建議, 加上解析器BeautifulSoup(data.text,features="lxml") , 但這個有時候會有造成程式出錯的風險, 因為有一些warnings的建議並不是寫得非常仔細
  3. 利用warnings套件, 選擇不要SHOW出任何WARNINGS, 眼不見為淨.可嘗試以下加在程式裡 import warnings warnings.filterwarnings('ignore')
erichuang888 commented 2 years ago

已順利解決,謝謝

arleigh418 commented 2 years ago

不會, 歡迎您隨時提出討論.