2013-11-01 46 views
-1

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

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

大约一半的时候,我只想捕捉到10月30日在这个车站的最高阵风速度为23英里/小时。

有可能用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]) 
+3

这是软件,所以答案几乎总是“是的,这是可能的。” –

回答

2

这听起来像是你想刮一个网站。在这种情况下,我会使用Python的urllib和美丽的汤lib。

编辑:

我只是看看你的链接,我不认为美丽的汤真的会在这种情况下无关紧要。我仍然会使用urllib,但是一旦你获得了这个对象,你就必须解析这些数据来寻找你需要的东西。这有点哈克,但应该工作。我必须回头看看事情是如何发生的。

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

一旦你得到这些数据,你可以创建任何你想要检查的逻辑,如果前一个值大于你的最后一遍。一旦你找出那部分,出去并获取数据。只需创建一个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 
+0

酷感谢我会试试! – Andrew

相关问题