惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
T
Threat Research - Cisco Blogs
量子位
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
F
Fortinet All Blogs
爱范儿
爱范儿
D
Docker
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
A
About on SuperTechFans
P
Proofpoint News Feed
博客园 - 司徒正美
Recent Announcements
Recent Announcements
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Hacker News: Ask HN
Hacker News: Ask HN
AI
AI
博客园_首页
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
C
Check Point Blog
S
Security Affairs
L
LINUX DO - 最新话题
大猫的无限游戏
大猫的无限游戏
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
Y
Y Combinator Blog

博客园 - hgdfr

Django1.5内置的用户认证系统介绍(之五)在admin后台管理用户 --by hillfree Django1.5内置的用户认证系统介绍(之四)Authentication in Web requests --by hillfree Django1.5内置的用户认证系统介绍(之三)权限与授权--by hillfree Django1.5内置的用户认证系统介绍(之二)使用User对象--by hillfree Django1.5内置的用户认证系统介绍(之一)--by hillfree Django REST Framework Tutorial 5:关系与超链接API(中文版教程)by hillfree Django REST Framework Tutorial 4:认证与权限(中文版教程)by hillfree Django REST Framework Tutorial 3:基于类的Views(中文版教程)by hillfree Django1.5+Python3.3下groundwork的使用 Django1.5中设置静态目录的简要说明 Python3.3中如何产生伪随机数 《鲜活的数据:数据可视化指南》第2章 收集数据 Python3.3源码 CSCW领域的“老”词和“新”词 FOAF简介和朋友圈子 Class Library类型的工程难道不能用app.config? 配置文件中的DataDirectory在那里设置? 自己写的类需要重写ToString(), HashCode(), Equal()吗? 注释中如果出现尖括号怎么办?“<” - hgdfr - 博客园 Access Control Issues 有关访问控制
参照《鲜活的数据:数据可视化指南》第2章:抓取网页数据(历史天气记录)的Python程序
hgdfr · 2013-03-19 · via 博客园 - hgdfr

之前在:《鲜活的数据:数据可视化指南》第2章 收集数据 Python3.3源码 中改写了原书的代码为python3.3版本,为了更好的学习和熟悉BeautifulSoup,又改编了一个程序,从中文网站http://lishi.tianqi.com/beijing/...上来下载相关天气信息,并保存为CSV格式。

 1 # coding = utf-8 
 2 """
 3 从http://lishi.tianqi.com/beijing/下载部分北京天气记录保存到文件中。
 4 利用BeautifulSoup模块
 5 """
 6 __author__ = 'hillfree'
 7 
 8 
 9 
10 from urllib import request
11 from bs4 import BeautifulSoup
12 
13 
14 def downloadPages():
15     # Iterate through year, months and day
16     for year in range(2011, 2014):
17         for month in range(1, 13):
18 
19             # 目前数据只提供从2011年1月至2013年3月的数据
20             if year == 2013 and month == 4:
21                 break
22 
23             stamp = "{0}{1:02d}".format(year, month)
24             url = "http://lishi.tianqi.com/beijing/{0}.html".format(stamp,)
25             filename = "BeijingWeather{0}.html".format(stamp,)
26 
27             # 方法1: 直接使用urlretrieve()。 推荐
28             request.urlretrieve(url, filename)
29             print(stamp + "OK!")
30 
31             # 方法2: 分别打开链接和文件写入
32             # page = request.urlopen(url)
33             # print(url + " Open OK!")
34             # # 注意此处一定用‘wb’打开文件
35             # file = open(filename, 'wb')
36             # file.write(page.read())
37             # file.close()
38             # print(filename + "Save OK!")
39             # page.close()
40 
41 def parsePageToCSV():
42     output = open("BeijingWeather.csv", 'w', encoding="utf-8")
43     for year in range(2011, 2014):
44         for month in range(1, 13):
45 
46             # 目前数据只提供从2011年1月至2013年3月的数据
47             if year == 2013 and month == 4:
48                 break
49 
50             stamp = "{0}{1:02d}".format(year, month)
51             filename = "BeijingWeather{0}.html".format(stamp,)
52 
53             file = open(filename, 'r')
54             soup = BeautifulSoup(file)
55 
56 
57             monthData = soup.find(name="div", attrs={"class":"tqtongji2"});
58             if monthData == None:
59                 break
60             uls = monthData.findAll("ul")[1:]   # 滤去表头汉字
61             for ul in uls:
62                 lis = ul.findAll("li")
63                 # 依次取出日期、最高气温、最低气温、天气、风向、风力
64                 item = "{0}, {1}, {2}, {3}, {4}, {5}".format(lis[0].string, lis[1].string,
65                                                              lis[2].string, lis[3].string, lis[4].string, lis[5].string, )
66                 print(item)
67                 output.write(item + "\n")
68 
69     output.close()
70 
71 
72 if __name__ == "__main__":
73     downloadPages()
74     parsePageToCSV()