Pergunta

I'm not able to filter the results of table[3] to only include rows that have today's date in them. I'm using this url as my data source:

http://tides.mobilegeographics.com/locations/3881.html

I can get all the data back, but my filtering isn't working. I get the entire range, 5 days back. I only want something like this: (current day)

Montauk Point, Long Island Sound, New York
41.0717° N, 71.8567° W

2014-03-13 12:37 PM EDT   0.13 feet  Low Tide
2014-03-13  6:51 PM EDT   Sunset
2014-03-13  7:13 PM EDT   2.30 feet  High Tide

How can I get this and then calculate if the tide is moving in/out within next 40 minutes.

Thanks for helping.

My Code is:

import sre, urllib2, sys, BaseHTTPServer, datetime, re, time, pprint, smtplib
from bs4 import BeautifulSoup
from bs4.diagnose import diagnose

data = urllib2.urlopen('http://tides.mobilegeographics.com/locations/3881.html').read()
day = datetime.date.today().day
month = datetime.date.today().month

year = datetime.date.today().year
date = datetime.date.today()
soup = BeautifulSoup(data)

keyinfo = soup.find_all('h2')
str_date = datetime.date.today().strftime("%Y-%m-%d")
time_text = datetime.datetime.now() + datetime.timedelta(minutes = 20)

t_day = time_text.strftime("%Y-%m-%d")
tide_table = soup.find_all('table')[3]
pre = tide_table.findAll('pre')

dailytide = []
pattern = str_date
allmatches = re.findall(r'pattern', pre)
print allmatches

if allmatches:
    print allmatches
else:
    print "Match for " + str_date + " not found in data string \n" + datah
Foi útil?

Solução

You don't need a regular expression, just split the contents of a pre tag and check if today's date is in the line:

import urllib2
import datetime
from bs4 import BeautifulSoup


URL = 'http://tides.mobilegeographics.com/locations/3881.html'
soup = BeautifulSoup(urllib2.urlopen(URL))
pre = soup.find_all('table')[3].find('pre').text

today = datetime.date.today().strftime("%Y-%m-%d")
for line in pre.split('\n'):
    if today in line:
        print line

prints:

2014-03-13  6:52 PM EDT   Sunset
2014-03-13  7:13 PM EDT   2.30 feet  High Tide
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top