简体   繁体   English

文件输出与终端输出不同

[英]File output different to terminal output

I'm trying to write a program that reads an online calendar (for Coles Employees) and writes the output to an ICS file for calendar applications. 我正在尝试编写一个程序(读取在线日历(适用于Coles员工),并将输出写入ICS文件以用于日历应用程序)。

I've gotten to the stage where I want to read the page source and sift through it to find the shifts that are rostered. 我已经到了要阅读页面源代码并进行筛选以查找排班名册的阶段。

My only problem is when trying to output these to a file, the output that shows in my terminal when printing (which is correct) is different to the output that gets written to my output file. 我唯一的问题是尝试将这些输出到文件时,打印时在终端上显示的输出(正确)与写入我的输出文件的输出不同。

# this is how i collect the page source #
from webbot import Browser

web =  Browser()
web.go_to('https://login.colesgroup.com.au/nidp/saml2/sso?sid=0&option=credential')
web.type('********') #username
web.press(web.Key.TAB)
web.type('********')#password
web.click(id = 'button')
web.click(id = 'a.actionn-item')
web.go_to("https://colesgroup.sharepoint.com/sites/mycoles/Pages/redirect.aspx?feature=myhours")
web.click('Shift Details View')

data = web.get_page_source()



with open('pagesource.txt', 'w') as file:

    file.write(data)

#in another file #
with open("pagesource.txt", 'r') as file:
    for line in file:
        if 'roster-timeblock-home' in line:
            print(line)
            output = line

with open("shifts.txt", 'w') as file:
                file.write(output) # The output that gets written in shifts.txt is different to the terminal output#

The output in shifts.txt should be the same as the terminal output, here shifts.txt中的输出应与终端输出相同,此处

<div class="roster-timeblock roster-timeblock-home" data-date="2019-16-07" style="top: 66.6667%; height: 12.5%; opacity: 1;"><div class="roster-timeblock-time-wrapper"><span class="roster-timeblock-time">16:00<br />19:00</span></div></div></div>

<div class="roster-timeblock roster-timeblock-home" data-date="2019-21-07" style="top: 50%; height: 33.3333%; opacity: 1;"><div class="roster-timeblock-time-wrapper"><span class="roster-timeblock-time">12:00<br />20:00</span></div></div></div>

However, this is what I get 但是,这就是我得到的

<div class="roster-timeblock roster-timeblock-home" data-date="2019-21-07" style="top: 50%; height: 33.3333%; opacity: 1;"><div class="roster-timeblock-time-wrapper"><span class="roster-timeblock-time">12:00<br />20:00</span></div></div></div>

Only the second output... Can I get some help? 仅第二个输出...我可以得到一些帮助吗? Where am I going wrong? 我要去哪里错了?

You can see below a possible solution: 您可以在下面看到一个可能的解决方案:

with open("pagesource.txt", 'r') as file:
    results = []
    for line in file:
        if 'roster-timeblock-home' in line:
            print(line)
            results.append(line)  # Here if you use a simple variable, it will be overwritten in case of every new match.

with open("shifts.txt", 'w') as file:
                file.write("\n".join(results))

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

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