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

推荐订阅源

B
Blog RSS Feed
L
LangChain Blog
博客园_首页
量子位
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
T
Threatpost
N
Netflix TechBlog - Medium
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
C
Cisco Blogs
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
U
Unit 42
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
WordPress大学
WordPress大学
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
F
Full Disclosure
S
Security @ Cisco Blogs
T
Tor Project blog
V
V2EX
Y
Y Combinator Blog
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
B
Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

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