0

Hello all,

by using Python, i want to pickup the news titles on http://www.boston.com/yourtown/ and save them into a .txt file.

i have 2 questions:
1. how can i have the codes run every 2 hours?
2. how can i have the outputs write into a .txt file?

thanks.

from bs4 import BeautifulSoup
import re
import urllib2



url = "http://www.boston.com/yourtown/"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

news_titles = soup.find_all('h4')

for nts in news_titles:
    for n in nts("a"):
        print n.renderContents()
3
Contributors
3
Replies
42
Views
4 Years
Discussion Span
Last Post by snippsat
0

please search for cron jobs in python.Cron jobs are task to be performed at specified time.That will solve your problem.Refer this

1

Python has a scheduler module,wish is a option to OS scheduler like Cron or Windows task scheduler.
I have used it's some times and it work well sleeping in background and do work only when it shall.
One positive aspect is that is crossplatform.

So this will run every 2 hours.

from bs4 import BeautifulSoup
import re
import urllib2
import sched, time

def do_event():
    url = "http://www.boston.com/yourtown/"
    page = urllib2.urlopen(url)
    soup = BeautifulSoup(page.read())
    news_titles = soup.find_all('h4')
    for nts in news_titles:
        for n in nts("a"):
            print n.renderContents()
    s.enter(7200, 1, do_event, ())

s = sched.scheduler(time.time, time.sleep)
s.enter(7200, 1, do_event, ())
s.run()

Edited by snippsat

Votes + Comments
good idea
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.