【问题标题】:How can I make use of dictionary in order to store multiple values in two different arrays如何使用字典将多个值存储在两个不同的数组中
【发布时间】:2019-10-23 16:01:32
【问题描述】:

我想将 INFOSYS 和 RELIANCE 的衍生品报价的最后交易价格值存储在两个不同的列表中。之后,我希望我的程序从各自的列表中减去两个最新的值,并将输出作为值之间的差值提供。给定的代码为一个衍生报价提供输出。

如何使用单个代码从多个列表中为我提供所需的输出?我可以用字典来解决这个问题吗?

import requests
import json
import time
from bs4 import BeautifulSoup as bs
import datetime, threading


LTP_arr=[0]
url = 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-'

def ltpwap():
    resp = requests.get(url)
    soup = bs(resp.content, 'lxml')
    data = json.loads(soup.select_one('#responseDiv').text.strip())

    LTP=data['data'][0]['lastPrice']
    n2=float(LTP.replace(',', ''))

    LTP_arr.append(n2)
    LTP1= LTP_arr[-1] - LTP_arr[-2]

    print("Difference between the latest two values of INFY is ",LTP1)
    threading.Timer(1, ltpwap).start()

ltpwap()

产生:

Difference between the latest two values of INFY is 4.

预期结果是:

INFY_list = (729, 730, 731, 732, 733)
RELIANCE_list = (1330, 1331, 1332, 1333, 1334)    

【问题讨论】:

  • 我完全听不懂这个问题。请提供预期结果
  • 如何将多个脚本的值存储在不同的多个列表中
  • 预期结果:INFY_list=(729,730,731,732,733) RELIANCE_list=(1330,1331,1332,1333,1334)
  • 请不要在 cmets 中发布代码,edit 你的问题代替
  • 如何利用字典将值存储在两个不同的列表中,其中包含动态变化的票据的最后价格?

标签: python json beautifulsoup


【解决方案1】:

比保留一些列表更好的方法是让生成器在某个时间间隔从 URL 生成所需的值。这是time.sleep() 的实现,但我建议查看asyncio 的许多URL:

from bs4 import BeautifulSoup as bs
import json
import requests
from collections import defaultdict
from time import sleep

urls_to_watch = {
    'INFOSYS': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-',
    'RELIANCE': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=RELIANCE&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-'
}

def get_values(urls):
    for name, url in urls.items():
        resp = requests.get(url)
        soup = bs(resp.content, 'lxml')
        data = json.loads(soup.select_one('#responseDiv').text.strip())

        LTP=data['data'][0]['lastPrice']
        n2=float(LTP.replace(',', ''))
        yield name, n2

def check_changes(urls):
    last_values = defaultdict(int)
    current_values = {}
    while True:
        current_values = dict(get_values(urls))
        for name in urls.keys():
            if current_values[name] - last_values[name]:
                yield name, current_values[name], last_values[name]
        last_values = current_values
        sleep(1)

for name, current, last in check_changes(urls_to_watch):
    # here you can just print the values, or store current value to list
    # and periodically store it to DB, etc.
    print(name, current, last, current - last)

打印:

INFOSYS 750.7 0 750.7
RELIANCE 1284.4 0 1284.4
RELIANCE 1284.8 1284.4 0.3999999999998636
INFOSYS 749.8 750.7 -0.900000000000091
RELIANCE 1285.4 1284.8 0.6000000000001364

...and waits infinitely for any change to occur, then prints it.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2021-01-29
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多