python爬取中国天气网七日天气信息

采用requests库爬取中国天气网七日天气信息,爬取完毕后将信息写入文件

import requests
from lxml import etree
def getWeatherInfo(url):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'cookie': '登录网站后cookie填这里'
    }
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding
    html = etree.HTML(response.text)
    all_weather = html.xpath('/html/body/div[4]/div[2]/div/div/div[2]/div')
    dates=html.xpath('/html/body/div[4]/div[2]/div/div/ul[2]/li')
    date=[]
    for d in dates:
        date.append(d.xpath('./text()')[0].split('(')[1].strip(')'))
    for index1,info in enumerate(all_weather):
        conMidtab2 = info.xpath("./div")
        for conmid2 in conMidtab2:
            tr_list = conmid2.xpath('./table/tr')[2:]
            for index, tr in enumerate(tr_list):
                if index == 0:
                    provice = tr.xpath('./td[1]/a/text()')[0]
                    city = tr.xpath('./td[2]/a/text()')[0]
                    weather = tr.xpath('./td[3]/text()')[0]
                    wind = tr.xpath('./td[4]/span[1]/text()')[0]
                    wind_grade = tr.xpath('./td[4]/span[2]/text()')[0]
                    temperature = tr.xpath('./td[5]/text()')[0]
                    print(provice, city, weather, wind, wind_grade, temperature,date[index1])
                else:
                    city = tr.xpath('./td[1]/a/text()')[0]
                    weather = tr.xpath('./td[2]/text()')[0]
                    wind = tr.xpath('./td[3]/span[1]/text()')[0]
                    wind_grade = tr.xpath('./td[3]/span[2]/text()')[0]
                    temperature = tr.xpath('./td[4]/text()')[0]
                    print(provice, city, weather, wind, wind_grade, temperature,date[index1])
                    with open("dayofzgweather.csv", 'a', encoding='utf-8') as f:
                        f.write("{},{},{},{},{},{},{}".format(provice, city, weather, wind, wind_grade, temperature,date[index1]))
                        f.write('\n')

if __name__ == '__main__':
    dic={'华北':'hb','东北':'db','华东':'hd','华中':'hz','华南':'hn','西北':'xb','西南':'xn'}
    area=input("请输入地区:")
    url=f'http://www.weather.com.cn/textFC/{dic[area]}.shtml'
    print(url)
    getWeatherInfo(url)









  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解如何用 Python 历史天气数据,是吗? 首先,需要确定您想要哪个地区的历史天气数据。一般来说,可以通过查询该地区的气象局或气象站获历史天气数据。 接着,可以使用 Python 中的 requests 库来发送 HTTP 请求,获页源代码。然后,可以使用 BeautifulSoup 库来解析页源代码,提出所需的天气数据。 以下是一个简单的示例代码,用于中国天气上北京市 2019 年 1 月份的历史天气数据: ``` python import requests from bs4 import BeautifulSoup url = 'http://www.weather.com.cn/weather/101010100.shtml#dt' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} r = requests.get(url, headers=headers) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, 'html.parser') weather_list = soup.select('.t li') for weather in weather_list: date = weather.select('.time')[0].text weather_detail = weather.select('.temp')[0].text.strip() print(date, weather_detail) ``` 以上代码中,我们首先通过 requests 库发送了一个 GET 请求,获了北京市天气页源代码。然后,使用 BeautifulSoup 库解析页源代码,提出了 2019 年 1 月份的天气数据,并打印输出了期和天气详情。 需要注意的是,不同的页结构不同,需要根据具体情况修改代码。同时,需要注意站的 robots.txt 文件,不要过度访问站,以免被封 IP 或其他限制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值