简体   繁体   English

写入新行 - Xlsxwriter

[英]Writing to new row - Xlsxwriter

First time posting be gentle as I'm currently learning Python on the fly at work.第一次发帖要温柔,因为我目前正在工作中学习 Python。 :) :)

Anywho, I decided to try and write something that will print a stock price every 2 minutes to Excel, using xlsxwriter and Selenium to get the webpage.任何人,我决定尝试编写一些东西,使用 xlsxwriter 和 Selenium 将每 2 分钟打印一次股票价格到 Excel 以获取网页。

My issue here is, that when it writes to excel only the first Rows are populated.我的问题是,当它写入 excel 时,只有第一行被填充。 When the 2 minutes are up, the new price (if there is one) wont write to excel on a new line.当 2 分钟结束时,新价格(如果有)不会在新行上写入 excel。

Any help is gratefully appreciated of course - Really looking forward to being part of the community: :)当然,任何帮助都将不胜感激 - 真的很期待成为社区的一员::)

Cheers guys!干杯伙计们!

Code is:代码是:

times = time.strftime('%H:%M:%S')
workbook = xlsxwriter.Workbook(r'\\dub-ppfs-001\Home\rglennon\Desktop\Firefox_Web_driver\Stock_Price.xlsx')
sheet = workbook.add_worksheet()
col = 1
row = 0


browser = webdriver.Firefox()
browser.get('https://www.marketwatch.com/investing/stock/pypl')

WebDriverWait(browser, 60, 5).until(ec.visibility_of_all_elements_located((By.XPATH, '//*[@class="value"]')))
browser.minimize_window()
pypl = browser.find_element_by_xpath('//*[@class="value"]')
openprice = (browser.find_element_by_xpath('//*[@class="intraday__close"]').text)


while True:
    for row in range(1):
        sheet.write(row, 0, pypl.text)
        row =+1
    for col in range(1):
        sheet.write(col, 1, times)
         col =+1
    if pypl.text >= '105.50':
        ps('metal.mp3')



    workbook.close()
    time.sleep(120)     

Fixed it with the help above: Thank you kindly :)在上面的帮助下修复了它:谢谢你:)

just had to alter the loop really:只需要真正改变循环:

try:

pypl = browser.find_element_by_xpath('//*[@class="value"]')
openprice = browser.find_element_by_xpath('//*[@class="intraday__close"]').text




while True:
    pypl = browser.find_element_by_xpath('//*[@class="value"]')
    times = strftime('%H:%M:%S')

    sheet.write(row, 0,   pypl.text)
    row =row+1
    sheet.write(col, 1,   times)
    col =col+1

    print('PayPals current price is: $' +pypl.text)
    print('recorded at the time of : ' +times)
    print('\n')
    time.sleep(10)

    browser.refresh()
    WebDriverWait(browser, 60, 5).until(ec.visibility_of_all_elements_located((By.XPATH, '//*[@class="value"]')))
    pypl = browser.find_element_by_xpath('//*[@class="value"]')

except KeyboardInterrupt:
sheet.write_formula('D1', '=MAXA(A:A)')
workbook.close()


browser.quit()

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

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