码迷,mamicode.com
首页 > 编程语言 > 详细

python 获取一周的天气内容

时间:2015-12-13 13:57:20      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

     在学习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()

 

python 获取一周的天气内容

标签:

原文地址:http://www.cnblogs.com/xgcode/p/5042616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!