The csv does not record.

Asked 2 years ago, Updated 2 years ago, 32 views

I would like to create a program to record the Nikkei Stock Average by scraping, but the program below does not record it in csv. Why?
The following programs have been improved for Python 3 with reference to this article.

import urlib.request, urlib.error
from bs4 import BeautifulSoup
from datetime import datetime
import csv
import time

time_flag = True
while True:
    if datetime.now().minute!=59:
        time.sleep(58)
        continue
    f=open('nikkei_heiki.csv', 'a')
    writer=csv.writer(f, lineterminator='\n')
    while datetime.now().second!=59:
        time.sleep(1)
time.sleep(1)
csv_list = [ ]

time_=datetime.now().strftime("%Y/%m/%d%H:%M:%S")

csv_list.append(time_)
url="http://www.nikkei.com/markets/kabu/"
html=urllib.request.urlopen(url)
soup = BeautifulSoup(html, "html.parser")
span=soup.find_all("span")
Nikkei_heikin=""

for tag in span:
    try:
        string_=tag.get("class").pop(0)
        if string_in "mkc-stock_prices":
        nikkei_heikin=tag.string
        break
    except:
        pass


    print(time_,nikkei_heikin)
    csv_list.append(nikkei_heikin)
    writer.writerow(csv_list)
    f.close()

python python3

2022-09-29 22:22

1 Answers

The code in the question doesn't work until 59 minutes per hour, but it's hard to debug, so it's better to use the scraping part of Nikkei stock as a function.If you make it a function as shown below, the indentation is only strange and works with the one listed in the question.However, I fixed the f=open('nikkei_heiki.csv', 'a') and the code to open the file because if you don't use the try finally or use the with syntax, the file will open when an error occurs.

def get_kabu():
    csv_list = [ ]

    time_=datetime.now().strftime("%Y/%m/%d%H:%M:%S")
    csv_list.append(time_)
    url="http://www.nikkei.com/markets/kabu/"
    html=urllib.request.urlopen(url)
    soup = BeautifulSoup(html, "html.parser")
    span=soup.find_all("span")
    Nikkei_heikin=""

    for tag in span:
        try:
            string_=tag.get("class").pop(0)
            if string_in "mkc-stock_prices":
                nikkei_heikin=tag.string
                break
        except:
            pass

    print(time_,nikkei_heikin)
    csv_list.append(nikkei_heikin)
    with open('nikkei_heiki.csv', 'a') as f:
        writer=csv.writer(f, lineterminator='\n')
        writer.writerow(csv_list)

get_kabu()

Also, BeautifulSoup can search class along with tag, so you can easily call it as follows:

def get_kabu():
    time_=datetime.now().strftime("%Y/%m/%d%H:%M:%S")
    url="http://www.nikkei.com/markets/kabu/"
    html=urllib.request.urlopen(url)
    soup = BeautifulSoup(html, "html.parser")
    try:
        nikkei_heikin=soup.find("span", class_="mkc-stock_prices").text
    exceptAttributeError:
        Nikkei_heikin=""
    print(time_,nikkei_heikin)
    with open('nikkei_heiki.csv', 'a') as f:
        writer=csv.writer(f, lineterminator='\n')
        writer.writerow ([time_,nikkei_heikin])


2022-09-29 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.