天气情况信息

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值