selenium练习————天气

1. 访问天气查询网站(网址如下),查询江苏省天气 http://www.weather.com.cn/html/province/jiangsu.shtml

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

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.weather.com.cn/html/province/jiangsu.shtml")
import time
time.sleep(3)
driver.refresh()
element_keyword = driver.find_element_by_id("forecastID")
res = element_keyword.text
list1 = res.split("℃\n")
lowest = None
low_city = []
for i in list1:
    i = i.replace("℃", "")
    city = i.split("\n")[0]
    lowtem1 = int(i.split("\n")[1].split("/")[0])
    lowtem2 = int(i.split("\n")[1].split("/")[1])
    lowtem3 = min(lowtem1,lowtem2)
    if lowest == None or lowtem3 < lowest:
        lowest = lowtem3
        low_city = [city]
    elif lowtem3 == lowest:
        low_city.append(city)
print('温度最低为%s℃, 城市有%s' % (lowest, ' '.join(low_city)))

你可能感兴趣的:(python)