写到新行-Xlsxwriter

问题描述 投票:1回答:1

第一次发布时请保持柔和,因为我目前正在工作中实时学习Python。 :)

[Anywho,我决定尝试使用xlsxwriter和Selenium来编写一些能够每2分钟打印一次股票价格到Excel的内容,以获取网页。

我的问题是,当它写入excel时,只会填充第一行。 2分钟结束后,新价格(如果有的话)将不会在新行中写入excel。

当然,我们非常感谢您的帮助-非常希望成为社区的一份子! :)

干杯!

代码为:

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)     
python excel xlsxwriter stock
1个回答
0
投票

在上述帮助下解决了!谢谢你:)

只需要真的改变循环:

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()
© www.soinside.com 2019 - 2024. All rights reserved.