python 获取一周的天气内容

     在学习python,看到一篇获取天气的python脚本,http://xianglong.me/article/get-city-weather-using-python-script/,这个太麻烦了,用beautifulsoup写了一个简单的脚本,

# -*- coding: cp936 -*-
import sys
import httplib
import re
import requests
import json
import urllib
from bs4 import BeautifulSoup


def Wea():
    r = requests.get('http://weather.sina.com.cn')
    print r.status_code
    f = open('1.html','w+')
    f.write(r.content)
    f.close()
    soup = BeautifulSoup(open('1.html'),"lxml")
    #print soup.title.encode('gb18030')
    data = {}
    for link in soup.find_all('p',attrs ={"class":"wt_fc_c0_i_day"}):
        temp = link.find_next_sibling('p',attrs ={"class":"wt_fc_c0_i_temp"})
        wea  = link.find_next("img")
        data[link.text] = temp.text + wea['title']        
    for text in data:
        print text,data[text]
if __name__ == '__main__':
    Wea()

 

posted on 2015-12-13 13:23  xgcode  阅读(441)  评论(0)    收藏  举报