Selenium 访问天气查询网站(网址如下),查询江苏省天气


pip 安装Selenium Web driver Python 客户端库
1. 访问天气查询网站(网址如下),查询江苏省天气
http://www.weather.com.cn/html/province/jiangsu.shtml

2. 获取江苏所有城市的天气,并找出其中每天最低气温最低的城市,显示出来,比如
温度最低为12℃, 城市有连云港 盐城]

from selenium import webdriver
#chrome驱动 执行该步骤时selenium会去到指定路径将chrome driver执行起来
driver = webdriver.Chrome(r"D:\for myself\Google\Chrome\Application\chromedriver.exe")
#get方法 打开指定网址
driver.get('http://www.weather.com.cn/html/province/jiangsu.shtml')
print(driver.get_cookies())
ele = driver.find_element_by_id('forecastID')
print(ele.text)
citysWeather = ele.text.split('℃\n')
print(citysWeather)
lowest = None #记录目前最低温,先设置为none
lowestCity = []#温度最低城市列表
for one in citysWeather:
one = one.replace('℃','')#去掉摄氏度 南京\n25℃/34 南京\n25/34
print(one)
curcity = one.split('\n')[0] #南京\n25/34 [0]代表南京
lowweather = one.split('\n')[1].split('/')[0] #南京\n25/34 [1]代表25/34 [0]代表25
lowweather = int(lowweather)
#还没有最低温记录,或者发现气温更低的城市
#注意条件不能写反
if lowest ==None or lowweather<lowest:
lowest = lowweather
lowestCity = [curcity]
#温度和当前最低相同,加入列表
elif lowest == lowest:
lowestCity.append(curcity)
print('温度最低为%s℃,城市有%s'%(lowest,''.join(lowestCity)))



driver.quit()

猜你喜欢

转载自www.cnblogs.com/iamshasha/p/11100890.html