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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
J
Java Code Geeks
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
A
About on SuperTechFans
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Securelist
I
InfoQ
N
News and Events Feed by Topic
I
Intezer
A
Arctic Wolf
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Help Net Security
B
Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术
S
SegmentFault 最新的问题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
F
Fortinet All Blogs

博客园 - 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()