from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
# 实现反监测(三不)
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
# 实例化一个浏览器对象
bro = webdriver.Chrome(chrome_options=option)
bro.get('https://tianqi.2345.com/wea_history/59493.htm')
#点击年份
bro.find_element(By.XPATH,'//*[@id="js_yearVal"]').click()
bro.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/div/ul/li[11]/a').click()
sleep(3)
#开始爬虫
'''
一月:'/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/div[2]/div/ul/li[1]/a'
二月:'/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/div[2]/div/ul/li[2]/a'
十二月:'/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/div[2]/div/ul/li[12]/a'
'''
for x in range(1,13):
bro.find_element(By.XPATH,'//*[@id="js_monthVal"]').click()
bro.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[1]/div[1]/div[1]/div[3]/div[2]/div/ul/li[{}]/a'.format(x)).click()
sleep(4)
tr_list = bro.find_elements(By.XPATH,'//*[@id="weather-history"]/table/tbody')
for tr in tr_list:
weather_list = tr.text.split('\n')[1::]
# print(weather_list)
for i in range(len(weather_list)):
weather_day_list = weather_list[i]
# print(weather_day_list)
weather = weather_day_list.split(' ')
# print(weather)
#日期
date = weather[0]
# print(date)
#周几
weeks = weather[1]
#最高温度
high_temp = weather[2]
#最低温度
low_temp = weather[3]
#天气
weather_type = weather[4]
#风级
windy_lv = weather[5]
#空气指数
a = weather[6]
b = weather[-1]
air_index = a + b
# print(air_index)
file = open('深圳2021天气情况.csv','a', encoding='gb18030')
file.write(date+','+weeks+','+high_temp+','+low_temp+','+weather_type+','+windy_lv+','+air_index)
file.write('\n')
file.close()
bro.close()
天气情况信息
最新推荐文章于 2024-07-28 15:46:11 发布