【问题标题】:Yahoo Finance IndexError: list index out of rangeYahoo Finance IndexError:列表索引超出范围
【发布时间】:2021-07-08 17:18:11
【问题描述】:

我使用下面的代码从 Yahoo Finance 获取价格。直到最近都工作得很好。代码仍然运行,但没有返回值,而是告诉我:IndexError: list index out of range.

我尝试重置 xpath 无济于事。关于如何使它工作的想法?

import lxml.etree
import urllib.parse as urlparse 
import requests

def parse_url(url):
    tree = lxml.etree.fromstring(url, parser=lxml.etree.HTMLParser())
    tree.xpath('.//table[@class="W(100%)"]')
    return tree 

if __name__ == "__main__":
    urlivv = requests.get('http://finance.yahoo.com/q?s=IVV').text
    docivv = parse_url(urlivv)
    priceivv = docivv.xpath('//*[@id="quote-header-info"]/div[3]/div[1]/div/span[1]//text()')

ivv = priceivv[0]

【问题讨论】:

    标签: xpath lxml yahoo-finance


    【解决方案1】:

    看看 urlivv 包含的内容,我不相信您想要的信息在您正在使用的 html 中。

    
    In [6]: urlivv
    Out[6]: '<!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>'
    

    【讨论】:

    • 谢谢。知道什么会起作用吗?也许 xpath 不再适用于该网站?
    猜你喜欢
    • 2011-10-31
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多