Search code examples
pythonschedule

How to write data from the web to a CSV file every 10 min


Hello I'm fairly new to Python and web scraping in general, but I am trying to obtain a data value from a web site, writing it to a CSV file. This also works quite fine for me. My problem is that I would like the script to get the value like every hour and store it in the CSV file. So I am doing something wrong with the schedule commands, since obtaining the value and writing it to the CSV file works great but only when I press run. Here is the code I have tried.

import urllib2
from bs4 import BeautifulSoup
import csv
from datetime import datetime
import os
import schedule
import time


def job():

url = 'https://coinmarketcap.com/currencies/bitcoin-cash/'

page = urllib2.urlopen(url)

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

name_box = soup.find('span', attrs={'class': 'text-large2'})

bch_value = float(name_box.text.strip())

os.chdir('C:\Users\NIK\.spyder2\PythonScripts')

with open('BCH_kurs', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([bch_value, datetime.now()])

schedule.every(1).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)

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

Solution

  • schedule is an

    in-process scheduler for periodic jobs ( https://pypi.python.org/pypi/schedule )

    so schedule runs in a process. to start this process you have to use run and start the process in that schedule runs ...