Python3网络爬虫之抓取城市天气预报

import requests
from lxml import etree
import json
import os

# def get_city_list():

city_url = "https://j.i8tq.com/weather2020/search/city.js"
response = requests.get(city_url)
city_js = response.content.decode()
citys = city_js.split("=")[-1]
# print(citys)
city_dic = json.loads(citys)
# print(city_dic)
# print(city_dic.items())
city_list = []
for k,v in city_dic.items():
    # print(k)
    # print(v)
    for k1,v1 in v.items():
        for k2,v2 in v1.items():
            # print(v2)
            city_list.append(v2);
# print(city_list)
# return city_list

# get_city_list()

input_city = input("请输入你要查询的地区天气:")
status = False
for city in city_list:
    if input_city == city["NAMECN"]:
        status = True
        city_urls = "http://www.weather.com.cn/weather/{}.shtml".format(city["AREAID"])
        print("正则搜索你要查询 {} 的天气....".format(input_city))
        # return city_urls
        # print(city_urls)
        # 发起响应
        response = requests.get(city_urls)
        content = response.content.decode()
        # 提取数据列表
        html_str = etree.HTML(content)
        city = html_str.xpath("//div[@class='crumbs fl']/a[2]/text()")[0]
        town = html_str.xpath("//div[@class='crumbs fl']/a[3]/text()")[0]
        # print(city,town)

        li_list = html_str.xpath("//ul[@class='t clearfix']/li")
        weather_list = []
        for li in li_list:
            item = {}
            item["address"] = city + ">" + town
            item["week_date"] = li.xpath("./h1/text()")[0]
            item["max_temp"] = li.xpath("./p[@class='tem']/span/text()")[0]
            item["min_temp"] = li.xpath("./p[@class='tem']/i/text()")[0].split("℃")[0]
            item["wea"] = li.xpath("./p[@class='wea']/text()")[0]
            print(item)
            weather_list.append(item)

        # 保存成json文件
        file_path = "./source"
        if os.path.exists(file_path):
            with open(file_path + "/weather.json", "w", encoding="utf-8") as f:
                json.dump(weather_list, f, ensure_ascii=False, indent=2)
        else:
            os.mkdir(file_path)
            with open(file_path + "/weather.json", "w", encoding="utf-8") as f:
                json.dump(weather_list, f, ensure_ascii=False, indent=2)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值