import csv
import json

data = l

#파일을 한 번 쓴다.
with open('data.js', "w", encoding="UTF-8-sig") as f_write:
    json.dump(l, f_write, ensure_ascii=False, indent=4)

#파일을 다시 읽는다.
data = ""
with open('data.js', "r", encoding="UTF-8-sig") as f:
    line = f.readline()
    while line:
        data += line
        line = f.readline()

#파일에 변수명을 추가하여 다시 쓴다.
final_data = f"var data = {data};"
with open('data.js', "w", encoding="UTF-8-sig") as f_write:
    f_write.write(final_data)

전에 사용했던 전체 코드로 합치기

import requests
from bs4 import BeautifulSoup
import csv
import json

response = requests.get("<http://paullab.synology.me/stock.html>")

response.encoding = 'utf-8'
html = response.text

soup = BeautifulSoup(html, 'html.parser')

oneStep = soup.select('.main')[2]
twoStep = oneStep.select('tbody > tr')[1:]

날짜 = []
종가 = []
전일비 = []
거래량 = []

for i in twoStep:
    날짜.append(i.select('td')[0].text)
    종가.append(int(i.select('td')[1].text.replace(',', '')))
    전일비.append(int(i.select('td')[2].text.replace(',', '')))
    거래량.append(int(i.select('td')[6].text.replace(',', '')))

l = []

for i in range(len(날짜)):
    l.append({
        '날짜':날짜[i],
        '종가':종가[i],
        '전일비':전일비[i],
        '거래량':거래량[i],
        })

#파일을 한 번 쓴다.
with open('data.js', "w", encoding="UTF-8-sig") as f_write:
    json.dump(l, f_write, ensure_ascii=False, indent=4)

#파일을 다시 읽는다.
data = ""
with open('data.js', "r", encoding="UTF-8-sig") as f:
    line = f.readline()
    while line:
        data += line
        line = f.readline()

#파일에 변수명을 추가하여 다시 쓴다.
final_data = f"var data = {data};"
with open('data.js', "w", encoding="UTF-8-sig") as f_write:
    f_write.write(final_data)

main.yml 데이터를 수정합니다. 해당 yml 코드는 아래 코드에 의존하고 있습니다.

GitHub Push - GitHub Marketplace

스케줄러는 push로 바꾸겠습니다. push로 해야 기다리지 않고 actions를 사용할 수 있기 때문입니다.

name: helloGithubAction

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        # 개인 토큰을 사용할 것인지 말 것인지
        persist-credentials: false 
    - name: 1. pip 업그래이드
      run: python -m pip install --upgrade pip
    - name: 2. 환경 설정
      run: pip install -r requirements.txt
    - name: 3. 파이썬 실행
      run: python test.py
    - name: Commit files
      run: |
        git config --local user.email "paullabkorea@gmail.com"
        git config --local user.name "paullabkorea"
        git add .
        git commit -m "Run crawler and update current data"
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        # branch: "master"
        github_token: ${{ secrets.GITHUB_TOKEN }}