I'm asking you to print it out as a Python txt file (sys.stdout function)

Asked 2 years ago, Updated 2 years ago, 102 views

import urllib.request
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
from urllib import parse
from selenium import webdriver
from collections import OrderedDict
from datetime import datetime
import time
import math
import sys
import datetime
import numpy as np
import matplotlib.pyplot as plt
import schedule
sys.stdout = open('covid19resultida.txt','w')

def job():
    sys.stdout = open('covid19result.txt','w')

    url = "http://ncov.mohw.go.kr/bdBoardList_Real.do?brdId=1&brdGubun=13&ncvContSeq=&contSeq=&board_id=&gubun="

    fp = urllib.request.urlopen(url)

    source = fp.read() 
    fp.close()
    soup = BeautifulSoup(source, 'html.parser')
    now = datetime.datetime.now()
    nowDate = now.strftime('%Y年%m月%d日')
    nowTime=now.strftime('%H時%M分%S秒')
    print ("Number of confirmed cases of Wuhan pneumonia by region")
    print(nowDate)
    print(nowTime)
    print("-----------------------------------------------------------")
    for i in range(1,19):
        title_tag = soup.find('button',{"data-city": "map_city{}".format(i)})
        title_name = title_tag.find('span',{"class": "name"})
        title_num = title_tag.find('span',{"class": "num"})
        title_before = title_tag.find('span',{"class": "before"})

        print("Region name: ", title_name.text")
        print("Number of confirmed cases in the area: ", title_num.text")
        print("increase in confirmed cases compared to the previous day: ", title_before.text")
        print("-----------------------------------------------------------")

    sumnumchart = soup.find('ul',{"class": "cityinfo"})
    sumnum=sumnumchart.find('span',{"class": "num"})
    print("Cumulative number of confirmed cases: ", sum.text")
    sumbefore=soup.find('span',{"class": "sub_num red"})
    print("increase from the previous day: ", sumbefore.text")


schedule.every().day.at("14:51").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Question: If you just use sys.stdout without using schedule, it's a good output to txt file in the notepad, but I made a loop to print it out every certain time using schedule, and the file (txt file) is made, but nothing is inside. Is there anyone who can help me?

sys scheduling crawling

2022-09-20 19:39

1 Answers


def job():
    with open("covid19result.txt", "w", encoding="utf-8") as fo:

        url = "http://ncov.mohw.go.kr/bdBoardList_Real.do?brdId=1&brdGubun=13&ncvContSeq=&contSeq=&board_id=&gubun="

        fp = urllib.request.urlopen(url)

        source = fp.read()
        fp.close()
        soup = BeautifulSoup(source, "html.parser")
        now = datetime.datetime.now()
        nowDate = now.strftime("%Y年%m月%d日")
        nowTime = now.strftime("%H時%M分%S秒")
        print("Number of confirmed cases of Wuhan pneumonia by region", file=fo)
        print(nowDate, file=fo)
        print(nowTime, file=fo)
        print("-----------------------------------------------------------", file=fo)
        for i in range(1, 19):
            title_tag = soup.find("button", {"data-city": "map_city{}".format(i)})
            title_name = title_tag.find("span", {"class": "name"})
            title_num = title_tag.find("span", {"class": "num"})
            title_before = title_tag.find("span", {"class": "before"})

            print("Region name: ", title_name.text, file=fo)
            print("Number of confirmed cases in the area: ", title_num.text, file=fo)
            print("increase in confirmed cases compared to the previous day: ", title_before.text, file=fo)
            print("-----------------------------------------------------------", file=fo)

        sumnumchart = soup.find("ul", {"class": "cityinfo"})
        sumnum = sumnumchart.find("span", {"class": "num"})
        print("Cumulative number of confirmed cases: ", sum.text, file=fo)
        sumbefore = soup.find("span", {"class": "sub_num red"})
        print("increase from the previous day: ", sumbefore.text, file=fo)

Try changing it like this.

sys.stdout is standard input/output, but I don't know if it's right to overwrite it with another file pointer. What's more correct is to open the file and turn the file pointer over to the file= parameter of the print function.


2022-09-20 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.