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

推荐订阅源

A
About on SuperTechFans
D
DataBreaches.Net
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
S
Secure Thoughts
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
腾讯CDC
GbyAI
GbyAI
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI

博客园 - lykyl的自留地

树莓派编译安装opencv3 (2019.1.6更新) centos6 安装EPEL "检索COM类工厂中 CLSID为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070005" 问题的解决 centos6.x 安装pylucene (20161027改) - lykyl的自留地 趣味python编程之经典俄罗斯方块 python编码规范 改进uwsgi启动脚本,使其支持多个独立配置文件 在CentOS 6.5上安装python2.7 "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法 利用SHELL脚本实现文件完整性检测程序(1.2版更新) 获取linux服务器基本信息脚本 DELPHI实现关闭指定进程,自身防杀 linux使用心得(持续更新) vim使用心得(持续更新) NFS服务基本配置及使用 利用shell脚本实现计划任务功能 V1.2 利用shell脚本实现计划任务功能 rsync简明手册 linux bash script简明手册(持续更新)
基于python编写的天气抓取程序
lykyl的自留地 · 2013-08-22 · via 博客园 - lykyl的自留地

以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢。为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取最新的天气情况并生成静态JS供网站调用。由于初学python,程序有些地方写得不是很优雅,还望高手指正。

代码如下:

#!/usr/bin/env python
#coding:UTF-8

import urllib,os,datetime

def GetWeather(cityid):
  "获取指定城市的天气情况"
  #http://www.weather.com.cn/data/cityinfo/101110301.html
  #{"weatherinfo":{"city":"延 长","cityid":"101110301","temp1":"31℃","temp2":"18℃","weather":"多 云","img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}
  url="http://www.weather.com.cn/data/cityinfo/"+cityid+".html"
  Result=""
  try:
    web=urllib.urlopen(url)
    content=web.read().decode('utf-8').replace('"',"")
  except Exception,e:
    Result="error"
  if content.find("{weatherinfo") >=0:
    Items=content.replace("{weatherinfo:{","").replace("}}","").split(",")
    if len(Items)>=8:
      Result="<span class='weather'>"+Items[0].split(":")[1]+"&nbsp;"+Items[4].split(":")[1]+"&nbsp;"+Items[2].split(":")[1]+"&nbsp;/&nbsp;"+Items[3].split(":")[1]+"&nbsp;</span><img src='/images/weather/"+Items[5].split(":")[1]+"'>"+"&nbsp;<img src='/images/weather/"+Items[6].split(":")[1]+"'>"
  return Result

def CreateJS(FileName,Content):
  if len(Content)>10:
    now=datetime.datetime.now()
    try:
      fp=open(FileName,'w')
      fp.write('document.write("'+Content.encode("utf-8")+'");\n')
      fp.write('//'+now.strftime('%Y-%m-%d %H:%M:%S')+'\n')
      fp.close()
    except IOError:
      print "ioerror"

if __name__ == "__main__":
  Wcont=GetWeather("101110301")
  #print Wcont
  CreateJS("/weather.js",Wcont)

注:

1、城市代码可以到中国天气网上去查。

2、天气图标也可以在中国天气网的图标示例里去获取,这里就不提供了。

3、有同学表示,天气网的插件不是支持延后加载吗?嗯,是这样的。经本人实测在有些手机浏览器上会导致整个页面变空白,问题已提交给官方。