写入新行xlsxri

2024-09-12 10:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

第一次发帖要温柔,因为我现在正在工作中学习Python

不管是谁,我决定试着写一些东西,用xlsxwriter和Selenium把每2分钟一次的股价打印到Excel上

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

当然,我们非常感谢您的帮助,非常期待成为社区的一员

干杯伙计们

代码为:

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)     

Tags: textbrowsertimevaluecolelementfindfirefox
1条回答
网友
1楼 · 发布于 2024-09-12 10:23:48

在上面的帮助下修复了它!非常感谢:)

只是必须改变循环真的:

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()

相关问题 更多 >