• 20231120_python练习_天气网爬取城市近七天温度情况


    先根据城市名找到对应编码,然后获取近七天天气情况

    淄博 101120301 [‘20日(今天)’] [‘晴’] [‘7℃’] [‘\n’, ‘<3级’, ‘\n’]
    淄博 101120301 [‘21日(明天)’] [‘晴转阴’] [‘20℃’, ‘8℃’] [‘\n’, ‘<3级’, ‘\n’]
    淄博 101120301 [‘22日(后天)’] [‘多云’] [‘17℃’, ‘3℃’] [‘\n’, ‘<3级’, ‘\n’]
    淄博 101120301 [‘23日(周四)’] [‘晴’] [‘8℃’, ‘-5℃’] [‘\n’, ‘3-4级转<3级’, ‘\n’]
    淄博 101120301 [‘24日(周五)’] [‘晴转多云’] [‘5℃’, ‘-5℃’] [‘\n’, ‘<3级’, ‘\n’]
    淄博 101120301 [‘25日(周六)’] [‘阴转多云’] [‘9℃’, ‘0℃’] [‘\n’, ‘<3级’, ‘\n’]
    淄博 101120301 [‘26日(周日)’] [‘晴’] [‘12℃’, ‘-2℃’] [‘\n’, ‘<3级’, ‘\n’]

    import requests
    import json
    from lxml import etree
    
    city_dic = {}
    text_mw = '淄博'
    #将中文转置为字符
    text_id = str(text_mw.encode('utf-8')).upper().replace('\\X','%')[2:-1]
    print('text_id',text_id)
    
    url = 'http://toy1.weather.com.cn/search?cityname=' + text_id
    #设置请求头
    header={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.43",
        "Referer":"http://www.weather.com.cn/", 
        "Cookie":"Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1700397713; f_city=%E6%B5%8E%E5%8D%97%7C101120101%7C; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1700398953"
    }
    resp = requests.get(url=url,headers=header)
    print(resp.status_code)
    
    data = resp.text[1:-1]
    json_data = json.loads(data)[0]['ref'][0:9]
    #获取城市对应编码
    print(json_data)
    #淄博 101120301
    city_url = 'http://www.weather.com.cn/weather/' + json_data + '.shtml'
    print(city_url)
    city_resp = requests.get(url=city_url,headers=header)
    city_resp.encoding = city_resp.apparent_encoding
    html = etree.HTML(city_resp.text)
    node_all = html.xpath('//ul[@class="t clearfix"]/li')
    #print('节点名称',[i.tag for i in node_all])
    for node in node_all:
        day_text = node.xpath('.//h1/text()')
        wea_text = node.xpath('.//p[@class="wea"]/text()')
        tem_text = node.xpath('.//p[@class="tem"]//text()')
        for tem in tem_text:
            if tem in ('/','\n'):tem_text.remove(tem)
        win_text = node.xpath('.//p[@class="win"]//text()')
        for win in win_text:
            if win in ('/','\n'):win_text.remove(win)
        print(text_mw,json_data,day_text,wea_text,type(tem_text),tem_text,win_text)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    [数据结构]排序算法的性能比较
    iShot——Mac上功能最全的截图、录屏创造工具
    Docker快速入门
    解密hash算法:散列表、布隆过滤器和分布式一致性hash的原理与应用
    Docker查看容器的初始启动命令参数的常见几种方式
    如何基于亚马逊云科技打造高性能的 SQL 向量数据库 MyScale
    windows环境CLion调试SRS流媒体服务器源码
    AR智能眼镜:提升现场服务技能、效率与盈利能力的利器(一)
    [网鼎杯 2018]Comment git泄露 / 恢复 二次注入 .DS_Store bash_history文件查看
    无胁科技-TVD每日漏洞情报-2022-8-12
  • 原文地址:https://blog.csdn.net/szc_1985/article/details/134519132