本篇文章小编给大家分享一下python爬取气象台每日天气图代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.安装Selenium
Selenium是一个Web的自动化(测试)工具,它可以根据我们的指令,让浏览器执行自动加载页面,获取需要的数据等操作。
pip install selenium
2. 安装chromedriver
Selenium 自身并不具备浏览器的功能,Google的Chrome浏览器能方便的支持此项功能,需安装其驱动程序Chromedriver
下载地址:http://chromedriver.storage.googleapis.com/index.html
在google浏览器的地址栏输入‘chrome://version/’,可以查看版本信息,下载接近版本的就可以。
3.代码
从图里可以看到,向前翻页指令对应的id是'prev'
from selenium import webdriver ## 导入selenium的浏览器驱动接口 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import Select import time import os import urllib.request level=['地面','925hPa','850hPa','700hPa','500hPa','100hPa'] chrome_driver = '路径/chromedriver.exe' #chromedriver的文件位置 driver = webdriver.Chrome(executable_path = chrome_driver) #加载浏览器驱动 driver.get('http://www.nmc.cn/publish/observations/china/dm/weatherchart-h000.htm') #打开页面 time.sleep(1) #模拟鼠标选择高度层 for z in level: button1=driver.find_element_by_link_text(z) #通过link文字精确定位元素 action = ActionChains(driver).move_to_element(button1) #鼠标悬停在一个元素上 action.click(button1).perform() #鼠标单击 time.sleep(1) for p in range(0,6): #下载最近6个时次的天气图 str_p=str(p) #模拟鼠标选择时间 button2=driver.find_element_by_id('prev') #通过id精确定位元素 action = ActionChains(driver).move_to_element(button2) #鼠标悬停在一个元素上 action.click(button2).perform() #鼠标单击 time.sleep(1) #模拟鼠标选择图片 elem_pic = driver.find_element_by_id('imgpath') #通过id精确定位元素 action = ActionChains(driver).move_to_element(elem_pic) #action.context_click(elem_pic).perform() #鼠标右击 filename= str(elem_pic.get_attribute('src')).split('/')[-1].split('?')[0] #获取文件名 #获取图片src src1=elem_pic.get_attribute('src') if os.path.exists('存图路径/'+z+'') is not True : os.makedirs('存图路径/'+z+'') urllib.request.urlretrieve(src1 , '存图路径/'+z+'/'+filename) print(filename) time.sleep(1)
然后就可以轻松的爬取所有图片
太极熊猫2百度版 安卓版v1.7.1
下载黎明觉醒应用宝版 安卓版v1.111.1
下载暗影格斗3国际版 (Shadow Fight 3)最新版v1.40.3
下载王者战魂华为版 安卓版v3.6.1
下载放置魔法学院免广告版 v2.9.5
放置魔法学院内置Mod菜单是游戏的破解版本,在该版本中为玩家
万乘之国qq版本 安卓版v1.0.5
万乘之国qq版本是一款极具魅力的策略游戏,玩家们可以通过不同
动物餐厅国际服无限内购版 v12.9
动物餐厅国际服免广告版是游戏的破解版本,在该版本中为玩家去除
欧洲卡车司机 安卓版v3.2
欧洲卡车司机是款模拟经营游戏,这款游戏的画面还是蛮逼真的,给
疯狂大酒店 (Grand Hotel Mania)最新中文版v4.10.0.20
疯狂大酒店(Grand Hotel Mania)是一款好玩的