1. 首页
  2. Python

新手用python爬虫自己做天气预报查询

“u003Cdivu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002F5e7c0005d279929f2522″ img_width=”1280″ img_height=”852″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E最近小编在学习爬虫,就想找个东西练练手,小说、图片、音乐什么的都烂大街了,正好最近天气是越来越冷,小编窝家里自己敲了个天气简单查询的代码,请大家指正下!u003Cu002Fpu003Eu003Ch1u003Eu003Cstrongu003E先找目标,最终还是选用了中国天气网,url:http:u002Fu002Fwww.weather.com.cnu002Fu003Cu002Fstrongu003Eu003Cu002Fh1u003Eu003Cpu003Eu003Cbu003E找到目标后,开始分析网页,发现里面是支持查询,并且查询的城市和url又一定的规律,如下:u003Cu002Fbu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002F5e8300041c10ce6bebc6″ img_width=”747″ img_height=”693″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E百度下后,发现url中的数字是城市代码,这就简单了,先把代码弄到,然后做成字典。u003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F5e7c0005d9278682e321″ img_width=”383″ img_height=”495″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E然后,分析网页的元素,发现近7天的预报都已经整整齐齐的放在那里了,这就简单了,开始写代码吧!u003Cu002Fpu003Eu003Ch1u003E选定方法,这里小编用的是xpath,方便、快捷。u003Cbru003Eu003Cu002Fh1u003Eu003Cpu003E需要提前安装lxml库,然后导入u003Cu002Fpu003Eu003Cpreu003Eimport requestsu003Cbru003Efrom lxml import etreeu003Cu002Fpreu003Eu003Cpu003E为了方便查询,直接写input,然后拼接网址u003Cu002Fpu003Eu003Cpreu003Ecity = input(“请输入要查询的城市:”)u003Cbru003Ecity_code = city_code_list[city]u003Cbru003Ehome_page = ‘http:u002Fu002Fwww.weather.com.cn’u003Cbru003Eurl = home_page + ‘u002Fweatheru002F’ + city_code + ‘.shtml’u003Cu002Fpreu003Eu003Cpu003E解析拼接好的网址,这里小编用requests获取get内容u003Cbru003Eu003Cu002Fpu003Eu003Cpreu003Edef get_html(url):u003Cbru003Eheader = {‘User-Agent’: ‘Mozillau002F5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Geckou002F20100101 Firefoxu002F57.0’}u003Cbru003Ehtml = requests.get(url, headers=header)u003Cbru003Ehtml.encoding = ‘utf-8’u003Cbru003Ereturn html.textu003Cbru003Ehtml = etree.HTML(get_html(url))u003Cu002Fpreu003Eu003Cpu003E然后开始查找路径u003Cu002Fpu003Eu003Cpreu003Edate = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fh1u002Ftext()’.format(i))u003Cu002Fpreu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F5e810004222b0dc5deed” img_width=”695″ img_height=”629″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E由于7天的预报都是在li标签下的,所以写到循环中,print输出即完成!u003Cu002Fpu003Eu003Cpu003E最后效果如下:u003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F5e810004239e2b4cbde3″ img_width=”386″ img_height=”201″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E简单的功能实现,成就感满满的!u003Cu002Fpu003Eu003Cpu003E代码放上:u003Cu002Fpu003Eu003Cpreu003Ecity = input(“请输入要查询的城市:”)u003Cbru003Ecity_code = city_code_list[city]u003Cbru003Ehome_page = ‘http:u002Fu002Fwww.weather.com.cn’u003Cbru003Eurl = home_page + ‘u002Fweatheru002F’ + city_code + ‘.shtml’ #city_code + ‘.shtml’u003Cbru003Edef get_html(url):u003Cbru003Eheader = {‘User-Agent’: ‘Mozillau002F5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Geckou002F20100101 Firefoxu002F57.0’}u003Cbru003Ehtml = requests.get(url, headers=header)u003Cbru003Ehtml.encoding = ‘utf-8’u003Cbru003Ereturn html.textu003Cbru003Ehtml = etree.HTML(get_html(url))u003Cbru003Efor i in range(1,8):u003Cbru003Edate = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fh1u002Ftext()’.format(i))[0]#日期u003Cbru003Eweather = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fp[1]u002Ftext()’.format(i))[0]#天气u003Cbru003Etem1 = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fp[@class=”tem”]u002Fspanu002Ftext()’.format(i))u003Cbru003Etem2 = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fp[@class=”tem”]u002Fiu002Ftext()’.format(i))u003Cbru003Etem = tem1[0]+’u002F’+ tem2[0]#拼接温度最高u002F最低u003Cbru003Ewin1 = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fp[@class=”win”]u002Fiu002Ftext()’.format(i))u003Cbru003Ewin2 = html.xpath(‘u002Fu002Ful[@class=”t clearfix”]u002Fli[{}]u002Fp[@class=”win”]u002Femu002Fspan[1]u002F@title’.format(i))u003Cbru003Ewin = win2[0]+ win1[0]#拼接风力大小u003Cbru003Eprint(date,weather,tem,win)u003Cu002Fpreu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F5e7d0005265dd74bb7ad” img_width=”1280″ img_height=”852″ alt=”新手用python爬虫自己做天气预报查询” inline=”0″u003Eu003Cu002Fpu003Eu003Cpu003E喜欢就关注一波呗(^_^),想和小编一起学习Python的,可以私信小编哦。u003Cu002Fpu003Eu003Cu002Fdivu003E”

原文始发于:新手用python爬虫自己做天气预报查询

主题测试文章,只做测试使用。发布者:醒百年,转转请注明出处:http://www.cxybcw.com/13372.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code