python每日一博
2018-02-25 22:51
417 查看
今天学习了爬虫基本的访问数据,分析数据的方法
用到了json和urllib2两个模块
urllib2的主要功能是访问网站,从网站上面获取基本的数据。
获得的数据基本是以字符串形式进行保存
然后通过json模块对获得的字符串进行处理,处理成为能够被python所处理的数据结构。
今天遇到的最大问题是有关python中汉字编码的问题
似乎python2.x要想用汉字对比成功必须要用cp936进行编码# -*- coding: cp936 -*-
import urllib2
import json
from city import city
#city是一个已存在的文件 包含对应城市的访问编码
cityname=raw_input('你想查哪个城市的天气?\n')
citycode=city.get(cityname)
if citycode:
url=('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
content=urllib2.urlopen(url).read()
data=json.loads(content)
weatherinfo=data['weatherinfo']
maxtemp=weatherinfo['temp2']
mintemp=weatherinfo['temp1']
weather=weatherinfo['weather']
print '%s %s~%s'%(weather,mintemp,maxtemp)
用到了json和urllib2两个模块
urllib2的主要功能是访问网站,从网站上面获取基本的数据。
获得的数据基本是以字符串形式进行保存
然后通过json模块对获得的字符串进行处理,处理成为能够被python所处理的数据结构。
今天遇到的最大问题是有关python中汉字编码的问题
似乎python2.x要想用汉字对比成功必须要用cp936进行编码# -*- coding: cp936 -*-
import urllib2
import json
from city import city
#city是一个已存在的文件 包含对应城市的访问编码
cityname=raw_input('你想查哪个城市的天气?\n')
citycode=city.get(cityname)
if citycode:
url=('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
content=urllib2.urlopen(url).read()
data=json.loads(content)
weatherinfo=data['weatherinfo']
maxtemp=weatherinfo['temp2']
mintemp=weatherinfo['temp1']
weather=weatherinfo['weather']
print '%s %s~%s'%(weather,mintemp,maxtemp)
相关文章推荐
- Python 每日记录
- 利用Python将每日一句定时推送至微信的实现方法
- Python每日一练0004
- python每日一类(2):platform
- 每日一python
- Python抓取每日必应壁纸
- CTF 【每日一题20160630】PYTHON 字节码
- Python每日一练0007
- [Python] 抓取必应每日一图,设置为桌面壁纸
- Python每日练习 04 你有一个目录,装有很多图片,现在你要把它们的尺寸变成不都大于Iphone5分辨率的大小
- python学习之每日一题
- Python实现Android自动化每日构建框架
- Python每日一练0009
- Python3中的内置函数,实例讲解-每日持续更新
- [python每日一练]--0009:找出html里的链接
- Python每日一练0010
- [python每日一练]--0007:代码统计
- 从零开始编写简易python爬虫:每日房价的获取
- python每日一类(3):os和sys
- 基于pyqt5+python3.4的cups每日商户统计程序开发