-1

我希望从每个天气预报办公室捕获每天最高的阵风值。我没有找到任何表格数据,所以我想我只需要创建一个可以从网页中提取数据的脚本。

例如网页:http ://forecast.weather.gov/product.php?site=JAN&issuedby=ORD&product=CLI&format=CI&version=5&glossary=0

大约下到一半,我只想捕捉到 10 月 30 日在这个车站的“最高阵风速度”,它将是 23 MPH。

有没有可能用 Python 来做到这一点?我需要每天运行脚本来捕捉前一天所有气象站的最高阵风。

我想知道我是否可以用每个站的链接填充一个表格,然后从那里开始。谢谢你。


已编辑

我拼凑了这段似乎有效的代码。然而,我发现这些数据在 txt 文件中更容易处理。谢谢你。

import urllib2, csv

url="http://forecast.weather.gov/product.php?
site=JAN&issuedby=ORD&product=CLI&format=CI&version=5&glossary=0"

downloaded_data = urllib2.urlopen(url)

#csv_data = csv.reader(downloaded_data)

row2 = ''
for row in downloaded_data:
    row2 = row2 + row

start = row2.find('HIGHEST GUST SPEED   ') + 21
end = row2.find('HIGHEST GUST DIRECTION', start)

print int(row2[start:end])
4

1 回答 1

2

听起来你想抓取一个网站。在那种情况下,我会使用 python 的 urllib 和美丽的汤库。

编辑:

我刚刚查看了您的链接,我认为在这种情况下,漂亮的汤并不重要。我仍然会使用 urllib,但是一旦你得到了那个对象,你就必须解析那些数据来寻找你需要的东西。这有点hacky,但应该可以工作。我得回去看看事情是怎么回事。

但是,您可以使用漂亮的汤来提取纯文本,以使您的纯文本解析更容易一些?无论如何,只是一个想法!

获得该数据后,您可以创建任何想要检查前一个值是否大于上一次传递的逻辑。一旦你弄清楚了那部分,就出去获取数据。只需创建一个 init.d 脚本并忘记它。

# example urllib 
def requesturl(self, url):
    f = urllib.urlopen(url)
    html = f.read()
    return html

 # beautiful soup
def beautifyhtml(self, html):
    currentprice_id = 'yfs_l84_' + self.s.lower()
    current_change_id = 'yfs_c63_' + self.s.lower()
    current_percent_change_id = 'yfs_p43_' + self.s.lower()
    find = []
    find.append(currentprice_id)
    find.append(current_change_id)
    find.append(current_percent_change_id)
    soup = BeautifulSoup(html)
    # title of the sites - has stock quote
    #title = soup.title.string
    #print(title)
    # p is where the guts of the information I would want to get
    #soup.find_all('p')
    color = soup.find_all('span', id=current_change_id)[0].img['alt']    
    # drilled down version to get current price:
    found = []
    for item in find:
        found.append(soup.find_all('span', id=item)[0].string)
    found.insert(0, self.s.upper())
    found.append(color)
    return found
于 2013-11-01T18:34:00.160 回答