简体   繁体   English

Python:将值从更新的值追加到现有电子表格

[英]Python : Appending values to existing spreadsheet from updated values

What is working now 现在在做什么

In the code below, the first 100 rows are getting downloaded to Futures.xlsx from the website. 在下面的代码中,前100行已从网站下载到Futures.xlsx。

The website updates every 15 minutes refreshing new values just like a company stock. 该网站每15分钟更新一次,以刷新新的价值,就像公司股票一样。 Total rows are 100. 总行数为100。

How can I modify my code such as every time a website updates or through some counter continue adding 100 more updated values to Futures.xlsx file. 我如何修改代码,例如每次网站更新或通过某些计数器继续向Futures.xlsx文件中添加100个以上的更新值。 So I would say over one hour there would be 400 Rows. 因此,我会说一小时以上将有400行。

I have included first four lines of the code output. 我已经包含了代码输出的前四行。

Output 输出量

   Contracts    Markets     Open    High    Low    Last     Pct     Time
0  Oct 2018 (E)    NG.F27.E    2.777   2.785   2.774   2.782   +0.36%  20:00
1  Nov 2018 (E)    NG.F27.E    2.793   2.800   2.792   2.800   +0.32%  19:51
2  Dec 2018 (E)    NG.F27.E    2.887   2.893   2.886   2.891   +0.21%  19:52
3  Jan 2019 (E)    NG.F27.E    2.977   2.984   2.975   2.980   +0.20%  19:52

Code

 urllib.request import urlopen
 from bs4 import BeautifulSoup
 import requests
 import pandas as pd
 from pandas import ExcelWriter
 from pandas import ExcelFile
 import os

 url = "https://quotes.ino.com/exchanges/contracts.html?r=NYMEX_NG"
 res = requests.get(url)
 soup = BeautifulSoup(res.text, 'lxml')

 Markets = []
 Contracts =[]
 Opens =[]
 Highs =[]
 Lows =[]
 Lasts=[]
 Changes=[]
 Pcts=[]

 data_rows = soup.findAll('tr')[3:]

 for td in data_rows[:100]:
 Market = td.findAll ('td')[0].text
 Markets.append(Market)
 Contract = td.findAll('td')[1].text
 Contracts.append(Contract)
 Open = td.findAll('td')[2].text
 Opens.append(Open)
 High = td.findAll('td')[3].text
 Highs.append(High)
 Low = td.findAll('td')[4].text
 Lows.append(Low)
 Last = td.findAll('td')[5].text
 Lasts.append(Last)
 Change = td.findAll('td')[6].text
 Changes.append(Change)
 Pct = td.findAll('td')[7].text
 Pcts.append(Pct)
 Time = td.findAll('td')[8].text

 df = pd.DataFrame({'Contracts' :Contracts,    Markets':Market,'Open':Opens, 
           'High':Highs, 'Low':Lows,'Last':Lasts,'Pct':Pcts})

out_path = "C:\Sid\Futures.xls"
writer = pd.ExcelWriter(out_path , engine='xlsxwriter')
df.to_excel(writer,'Sheet2',index=False)
writer.save()

I would suggest using the time.sleep method to set a timer for every 15 minutes. 我建议使用time.sleep方法设置每15分钟的计时器。 You can set up a small function to refresh the program for you, which I have done below. 您可以设置一个小功能来为您刷新程序,下面我做了。

def RefreshProgram(Program, timespan):
    while Program.isRunning:
        program.refresh()
        sleep(900)

Below would be how you could go about saving your excel data. 以下是如何保存Excel数据的方法。 datalist would be a row of data that was taken from your web page. datalist将是从您的网页获取的一行数据。

logbook=pxl.load_workbook(file_location_goes_here, data_only=False)

emptylist=['','','','','','']

ash=logbook["name_of_sheet_goes_here"]

datalist=[data_from_webpage]

#defining row, column, and checking cell data

    rowx = 1
    colx = 1
    cellcontent = ash.cell(row=rowx, column=colx).value

    #finding first empty row           
    while cellcontent != None:
        rowx += 1
        cellcontent= ash.cell(row=rowx, column=colx).value

    for data in datalist:
        ash.cell(row=rowx, column=colx).value = data
        colval += 1

    logbook.save(filelocation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM