我需要从汤中提取数据

时间:2019-07-15 23:33:09

标签: beautifulsoup

我需要从汤品中获取数据

我尝试了一些python代码,但是没有用-我是python新手

import bs4
import requests
from bs4 import BeautifulSoup

r = requests.get('https://finviz.com/futures_performance.ashx')

soup = bs4.BeautifulSoup(r.text, "lxml")

soup.find_all('script')[14].text.strip()

我需要从以下格式获取数据: 'var rows',{"ticker", "label","group", "perf"}

1 个答案:

答案 0 :(得分:0)

此页面上的数据是通过AJAX从不同的URL(JSON消息)加载的。我们可以使用Python的内置json模块来解析它。所需的数据在变量data中。我用它来漂亮地打印值:

import requests
import json
from operator import itemgetter

url = 'https://finviz.com/api/futures_perf.ashx'

data = requests.get(url).json()

# Data is loaded in variable `data`. To print it, uncomment next line:
# print(json.dumps(data, indent=4))

print('{: ^15}{: ^15}{: ^15}{: >15}'.format('Ticker', 'Label', 'Group', 'Perf'))
print('-' * 15*4)
f = itemgetter('ticker', 'label', 'group', 'perf')
for ticker in data:
    print('{: ^15}{: ^15}{: ^15}{: >15}'.format(*f(ticker)))

打印:

    Ticker          Label          Group                Perf
------------------------------------------------------------
      KC           Coffee          SOFTS                3.09
      6N             NZD        CURRENCIES              0.24
      GC            Gold          METALS                0.13
      HO         Heating Oil      ENERGY                0.13
      QA       Crude Oil Brent    ENERGY                0.11
      NQ         Nasdaq 100       INDICES                0.1
      ES           S&P 500        INDICES               0.07
      ZB        30 Year Bond       BONDS                0.06
      ER2       Russell 2000      INDICES               0.06
      DY             DAX          INDICES               0.05
      ZN        10 Year Note       BONDS                0.04
      YM            DJIA          INDICES               0.03
      SI           Silver         METALS                0.03
      DX             USD        CURRENCIES              0.03
      CL        Crude Oil WTI     ENERGY                0.02
      PL          Platinum        METALS                0.01
      ZF         5 Year Note       BONDS                 0.0
      ZT         2 Year Note       BONDS                 0.0
      6A             AUD        CURRENCIES               0.0
      LC         Live Cattle       MEATS                 0.0
      PA          Palladium       METALS               -0.01
      6E             EUR        CURRENCIES             -0.01
      FC        Feeder Cattle      MEATS               -0.02
      6S             CHF        CURRENCIES             -0.03
      ZL         Soybean oil      GRAINS               -0.04
      RB        Gasoline RBOB     ENERGY               -0.04
      6C             CAD        CURRENCIES             -0.05
      HG           Copper         METALS               -0.06
      6B             GBP        CURRENCIES             -0.06
      6J             JPY        CURRENCIES             -0.08
      EX        Euro Stoxx 50     INDICES              -0.17
      VX             VIX          INDICES              -0.19
      RS           Canola         GRAINS               -0.25
      ZW            Wheat         GRAINS                -0.3
      ZM        Soybean Meal      GRAINS               -0.39
      LH          Lean Hogs        MEATS               -0.43
      NKD        Nikkei 225       INDICES              -0.44
      ZO            Oats          GRAINS               -0.44
      NG         Natural Gas      ENERGY               -0.46
      ZR         Rough Rice       GRAINS                -0.5
      ZS          Soybeans        GRAINS               -0.53
      CT           Cotton          SOFTS               -0.58
      ZC            Corn          GRAINS               -0.62
      JO        Orange Juice       SOFTS               -1.89
      SB            Sugar          SOFTS               -2.11
      ZK           Ethanol        ENERGY               -2.76
      CC            Cocoa          SOFTS               -3.08
      LB           Lumber          SOFTS               -3.59

变量data是项目列表:

[
    {
        "ticker": "KC",
        "label": "Coffee",
        "group": "SOFTS",
        "perf": 3.09
    },
    {
        "ticker": "6N",
        "label": "NZD",
        "group": "CURRENCIES",
        "perf": 0.22
    },
    {
        "ticker": "GC",
        "label": "Gold",
        "group": "METALS",
        "perf": 0.13
    },

... and so on.