来自雅虎的python lxml etree applet信息

Dr *_*ile 8 python lxml web-scraping python-3.x

雅虎财务更新了他们的网站 我有一个用于提取分析师建议的lxml/etree脚本.然而,现在,分析师的建议在那里,但仅作为图形.您可以在此页面上看到示例.右栏中名为"建议趋势"的图表显示了分析报告的数量,显示强买入,买入,持有,表现不佳和卖出.

我的猜测是雅虎会在接下来的一段时间内对页面进行一些调整,但它让我想知道这些数据是否可以以任何合理的方式提取?

  1. 我的意思是,有没有办法让图形与之一起工作?
  2. 即使一个人成功了,是否有合理的方法从图形中提取数据?

我以前得到这样的来源:

url = 'https://finance.yahoo.com/quote/'+code+'/analyst?p='+code
tree = etree.HTML(urllib.request.urlopen(url).read())
Run Code Online (Sandbox Code Playgroud)

然后在html树中查找数据.但显然现在这是不可能的.

saa*_*aaj 2

正如评论所说,他们已经转移到 ReactJS,所以lxml不再是重点,因为 HTML 页面中没有数据。现在您需要环顾四周并找到他们从中提取数据的端点。如果是推荐趋势,它就在那里。

#!/usr/bin/env python3


import json
from pprint import pprint
from urllib.request import urlopen
from urllib.parse import urlencode


def parse():
    host   = 'https://query2.finance.yahoo.com'
    path   = '/v10/finance/quoteSummary/CSX'
    params = {
        'formatted' : 'true',
        'lang'      : 'en-US',
        'region'    : 'US',
        'modules'   : 'recommendationTrend'
    }

    response = urlopen('{}{}?{}'.format(host, path, urlencode(params)))
    data = json.loads(response.read().decode())

    pprint(data)


if __name__ == '__main__':
    parse()
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样。

{
  'quoteSummary': {
    'error': None,
    'result': [{
      'recommendationTrend': {
        'maxAge': 86400,
        'trend': [{
            'buy': 0,
            'hold': 0,
            'period': '0w',
            'sell': 0,
            'strongBuy': 0,
            'strongSell': 0
          },
          {
            'buy': 0,
            'hold': 0,
            'period': '-1w',
            'sell': 0,
            'strongBuy': 0,
            'strongSell': 0
          },
          {
            'buy': 5,
            'hold': 12,
            'period': '0m',
            'sell': 2,
            'strongBuy': 6,
            'strongSell': 1
          },
          {
            'buy': 5,
            'hold': 12,
            'period': '-1m',
            'sell': 2,
            'strongBuy': 7,
            'strongSell': 1
          },
          {
            'buy': 6,
            'hold': 11,
            'period': '-2m',
            'sell': 2,
            'strongBuy': 8,
            'strongSell': 1
          },
          {
            'buy': 6,
            'hold': 11,
            'period': '-3m',
            'sell': 2,
            'strongBuy': 8,
            'strongSell': 1
          }]
        }
    }]
  }
}
Run Code Online (Sandbox Code Playgroud)

如何寻找数据

我所做的大致是:

  1. 在目标小部件中找到一些唯一的标记(例如图表值或趋势字符串)
  2. 页面的开源(使用一些 HTML 和 JS 格式化程序,例如this
  3. 在那里查找令牌(在第三页中是以 开头的部分/* -- Data -- */
  4. 搜索“.js”以获取脚本标签(或编程包含项,例如 require.js)并在那里查找令牌
  5. 在 Firebug 或 Chromium 开发者工具中打开网络选项卡并检查 XHR 请求
  6. 然后使用Postman(或者如果您喜欢终端则使用curl)去除额外的参数并查看端点的反应