close

【pandas_datareader】遇到RemoteDataError: Unable to read URL: https://finance...怎麼辦?五大方法幫你解決!

一、前言


你也在使用pandas_datareader時也出現下面這一大串東西嗎?遇到一樣的問題嗎?

這邊直接提供五大方法解決這個惱人的問題!

 

RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/2330.TW/history?period1=1468094400&period2=1625860799&interval=1d&frequency=1d&filter=history
Response Text:
b'<!DOCTYPE html>\n  <html lang="en-us"><head>\n  <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n      <meta charset="utf-8">\n      <title>Yahoo</title>\n      <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui">\n      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n      <style>\n  html {\n      height: 100%;\n  }\n  body {\n      background: #fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;\n      background-size: cover;\n      height: 100%;\n      text-align: center;\n      font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, sans-serif;\n  }\n  table {\n      height: 100%;\n      width: 100%;\n      table-layout: fixed;\n      border-collapse: collapse;\n      border-spacing: 0;\n      border: none;\n  }\n  h1 {\n      font-size: 42px;\n      font-weight: 400;\n      color: #400090;\n  }\n  p {\n      color: #1A1A1A;\n  }\n  #message-1 {\n      font-weight: bold;\n      margin: 0;\n  }\n  #message-2 {\n      display: inline-block;\n      *display: inline;\n      zoom: 1;\n      max-width: 17em;\n      _width: 17em;\n  }\n      </style>\n  <script>\n    document.write(\'<img src="//geo.yahoo.com/b?s=1197757129&t=\'+new Date().getTime()+\'&src=aws&err_url=\'+encodeURIComponent(document.URL)+\'&err=%<pssc>&test=\'+encodeURIComponent(\'%<{Bucket}cqh[:200]>\')+\'" width="0px" height="0px"/>\');var beacon = new Image();beacon.src="//bcn.fp.yahoo.com/p?s=1197757129&t="+new Date().getTime()+"&src=aws&err_url="+encodeURIComponent(document.URL)+"&err=%<pssc>&test="+encodeURIComponent(\'%<{Bucket}cqh[:200]>\');\n  </script>\n  </head>\n  <body>\n  <!-- status code : 404 -->\n  <!-- Not Found on Server -->\n  <table>\n  <tbody><tr>\n      <td>\n      <img src="https://s.yimg.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage.png" alt="Yahoo Logo">\n      <h1 style="margin-top:20px;">Will be right back...</h1>\n      <p id="message-1">Thank you for your patience.</p>\n      <p id="message-2">Our engineers are working quickly to resolve the issue.</p>\n      </td>\n  </tr>\n  </tbody></table>\n  </body></html>'

 

 

二、問題分析及解決辦法


問題分析:

因為pandas_datareader也是使用requset的方法去抓取yahoo finance的資料,所以猜測網站的網址或api等有所更動,導致套件包爬不出資料。

 

(1) 改使用fix-yahoo-finance    (yfinance)

這邊在pip install記得使用pip install yfinance --upgrade --no-cache-dir以免一直裝不進去唷。

# pip install yfinance --upgrade --no-cache-dir

import yfinance as yf 

df = yf.download('2330.TW') # 下載台積電資料

 

(2) 使用別得數據源替代

其他熱門公開取得數據的python套件還有investing及quandl,可以參考這兩篇:

【Python量化資料】用Python抓取Yahoo Finance、investing.com股價資料 python抓取資料最輕鬆的兩大方法

 

【Quandl】如何用Python取得國外期貨、股票、經濟數據資料

非常熱門也非常好用,都是我個人的愛用網站

 

(3) 自己寫一個def來爬取yahoo finance

其實是我最推薦的方法之一,畢竟如果你懂點爬蟲跟request這也是不錯的練習機會,可以自己寫一個方法,甚至套件。

這邊就不多囉嗦,如果還沒事甚麼概念的話,底下留言可以再出一篇手把手教學。

 

(4) 自己手動修理

我個人覺得與其去改別人的code不然自己手動寫,但確實也是一個方法。

 

(5) 靜靜等著別人修理好

如果你跟我一樣懶惰的話,那麼這個方法真的在適合你不過了,先使用(2)然後等別人修理好,再來用pip install --upgrade更新。

# pip install --upgrade pandas_datareader

 

 

三、後記


如果對上面有疑問或是想要更詳細教學的可以留言給我唷!有其他詢問也可以EMEAIL至我的信箱。

 

想要即時訂閱最新文章:

https://forms.gle/MdXmiF2HgHhNChL46

或者訂閱Telegram來接收最新資訊:

https://t.me/joinchat/VUUuDt67Uq5l6CIQ

arrow
arrow

    恩哥Python 發表在 痞客邦 留言(0) 人氣()