python3抓取中国天气网不同城市7天、15天实时数据
思路:
1、根据city.txt文档来获取不同城市code
2、获取中国天气网7d和15d不同城市url
3、利用requests库请求url获取html内容
4、利用beautifulsoup获取7d和15d指定天气数据
5、将获取的天气数据保存到csv文件中
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import csv '''
获取不同城市code
''' def get_citycode(city_name):
with open('city.txt', 'r', encoding='UTF-8') as fs:
lines = fs.readlines()#一次读取整个文件内容,且自动分成一行列表,readline()每次只读取一行
for line in lines:
if(city_name in line):
code = line.split('=')[0].strip()#每行去掉头尾空格,且用“=”分隔出code和cityname,返回字符串列表
return code
raise ValueError('invalid city name')#抛出异常 '''
获取不同城市7天url
''' def get_7d_url(city_name):
url = 'http://www.weather.com.cn/weather/'
code = get_citycode(city_name)
return url + code + '.shtml' '''
获取不同城市15天url
''' def get_15d_url(city_name):
url = 'http://www.weather.com.cn/weather15d/'
code = get_citycode(city_name)
return url + code + '.shtml' ''''
获取html内容
''' def get_content(url, data=None):
rep = requests.get(url, timeout=60)
rep.encoding = 'utf-8'
return rep.text '''
获取7天指定数据
''' def get_7d_data(htmltext, city):
content = []
bs = BeautifulSoup(htmltext, "html.parser")
body = bs.body
data = body.find('div', {'id': '7d'})
ul = data.find('ul')
li = ul.find_all('li')
for day in li:
line = [city]
date = day.find('h1').string
p = day.find_all('p')
text = p[0].string
if p[1].find('span') is None:
temperature_H = None
else:
temperature_H = p[1].find('span').string
temperature_L = p[1].find('i').string
wind_force = p[2].find('i').string
line.append(date)
line.append(text)
line.append(temperature_H)
line.append(temperature_L)
line.append(wind_force)
content.append(line)
return content '''
获取15天指定数据
''' def get_15d_data(htmltext, city):
content = []
bs = BeautifulSoup(htmltext, "html.parser")
body = bs.body
data = body.find('div', {'id': '15d'})
ul = data.find('ul')
li = ul.find_all('li')
for day in li:
line = [city]
span = day.find_all('span')
date = span[0].string
text = span[1].string
if span[2].find('em') is None:
temperature_H = None
else:
temperature_H = span[2].find('em').string
temperature_L = span[2].string
wind_direction = span[3].string
wind_force = span[4].string
line.append(date)
line.append(text)
line.append(temperature_H)
line.append(temperature_L)
line.append(wind_direction)
line.append(wind_force)
content.append(line)
return content '''
保存获取到的天气数据
csv文件
''' def save_data(data, filename):
with open(filename, 'a', errors='ignore', newline='') as f: #newline=" "是为了避免写入之后有空行
f_csv = csv.writer(f)
f_csv.writerows(data)#数据整行写入csv文件中 '''
爬取7天天气数据
'''
def _7d(city):
url = get_7d_url(city)
html = get_content(url)
result = get_7d_data(html,city)
save_data(result, 'E:\weather.csv') '''
爬取15天天气数据
'''
def _15d(city):
url = get_15d_url(city)
html = get_content(url)
result = get_15d_data(html,city)
save_data(result, 'E:\weather.csv') if __name__ == '__main__':
cities = input('city name: ').split(' ') # 键盘输入城市,用空格分隔开
for city in cities:
_7d(city)
_15d(city)
附:city.txt 获取地址:https://pan.baidu.com/s/1VNW8AJi6_zo7mP_90lTkiA 提取码:red5
python3抓取中国天气网不同城市7天、15天实时数据的更多相关文章
- Python爬取中国天气网
Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...
- 初识python 之 爬虫:爬取中国天气网数据
用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...
- scrapy实例:爬取中国天气网
1.创建项目 在你存放项目的目录下,按shift+鼠标右键打开命令行,输入命令创建项目: PS F:\ScrapyProject> scrapy startproject weather # w ...
- 吴裕雄--天生自然python爬虫:使用requests模块的get和post方式抓取中国旅游网站和有道翻译网站翻译内容数据
import requests url = 'http://www.cntour.cn/' strhtml = requests.get(url) print(strhtml.text) URL='h ...
- 中国天气网API接口
http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...
- 中国天气网-天气预报接口api
中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...
- 爬虫-通过本地IP地址从中国天气网爬取当前城市天气情况
1.问题描述 最近在做一个pyqt登录校园网的小项目,想在窗口的状态栏加上当天的天气情况,用爬虫可以很好的解决我的问题. 2.解决思路 考虑到所处位置的不同,需要先获取本地城市地址,然后作为中 ...
- 中国天气网 JSON接口的城市编码解析及结果
最近在弄一个Android应用,其中一个功能是天气情况展示,准备使用google的天气API服务(http://www.google.com/ig/api?hl=zh-cn&weather=, ...
- 第十二、模块二、调用中国天气网和qqOnline及TrainTimeWebService接口来突出Json方法
一. 浏览网页的时候,发送的请求.服务器反回来的永远是字符串,由于服务器后台使用的语言不通,所以就需要用工具反解,这里用到了json json方法一 json.loads()将字符串转化为python ...
随机推荐
- sh_09_字典的定义
sh_09_字典的定义 # 字典是一个无序的数据集合,使用print函数输出字典时,通常 # 输出的顺序和定义的顺序是不一致的! xiaoming = {"name": " ...
- vim文件编辑器
Vim 是 Vi 的增强版(在 Vi 的基础上增加了正则表达式的查找.多窗口的编辑等功能),使用 Vim 进行程序开发会更加方便. 想深入了解 Vi 和 Vim 的区别,可以在 Vim 命令模式下输入 ...
- AcWing:108. 奇数码问题(归并排序 + 逆序数)
你一定玩过八数码游戏,它实际上是在一个3×3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3×3的网格中. 例如: 5 2 8 1 3 _ 4 6 7 在游戏过程中,可以把空格与其上 ...
- 基于ElementUI,设置流体高度时,固定列与底部有间隙
基于ElementUI,设置流体高度时,固定列与底部有间隙问题,如下图: 解决办法: 1.fixed流体的高度设置为100% 2.将fixed的滚动内容的最大高度设置为none,bottom为 ...
- 利用Zookeeper实现分布式锁
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- hibernate一对多关联映射
一对多关联映射 映射原理 一对多关联映射和多对一关联映射的映射原理是一致的,都是在多的一端加入一个外键,指向一的一端.关联关系都是由多端维护,只是在写映射时发生了变化. 多对一和一对多的区别 多对一和 ...
- 关于慕课网《使用vue2.0实现购物车和地址选配功能》的总结
视频学习网址:http://www.imooc.com/learn/796 源码打包:https://codeload.github.com/fachaoshao/Vue-ShoppingCart/z ...
- spark 笔记 7: DAGScheduler
在前面的sparkContex和RDD都可以看到,真正的计算工作都是同过调用DAGScheduler的runjob方法来实现的.这是一个很重要的类.在看这个类实现之前,需要对actor模式有一点了解: ...
- directshow播放摄像头卡死问题
最近遇到一个坑,directshow显示摄像头的时候,使用无窗口模式结果在浏览器插件里面界面卡死,但是控制台下面的句柄传过去却能正常播放 刚开始以为是调用的参数问题,琢磨了几天硬是搞不定,最后想到插件 ...
- redhat下配置SEED DVS6446开发环境3
1.运行arm_v5t_le-gcc,报错为: 64bit的Linux操作系统,无法运行32bit的应用程序 /lib/ld-linux.so.2: bad ELF interpreter: No s ...