网络爬虫(一)--抓取天气预报网站城市信息

本篇博客介绍了一个Python脚本,该脚本使用urllib.request模块从天气网站爬取省市及区县信息,并将获取的数据整理成易于使用的字典格式。此过程包括按层级抓取省份、城市和区县代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import urllib.request

 
url1='http://m.weather.com.cn/data5/city.xml'
content1=urllib.request.urlopen(url1).read().decode('utf-8')
provinces=content1.split(',')
 
#抓省份
for p in provinces:
    p_code=p.split('|')[0]
    url2='http://m.weather.com.cn/data3/city%s.xml'%p_code
    content2=urllib.request.urlopen(url2).read().decode('utf-8')
    citys=content2.split(',')
 
#抓城市,限制了每个省份只抓3个城市
    for c in citys[:3]:
        c_code=c.split('|')[0]
        url3='http://m.weather.com.cn/data3/city%s.xml'%c_code
        content3=urllib.request.urlopen(url3).read().decode('utf-8')
        districts=content3.split(',')
 
#抓地区,限制每个城市只抓3个地区
        for d in districts[:3]:
            d_code=d.split('|')[0]
            name=d.split('|')[1]
            url4='http://m.weather.com.cn/data3/city%s.xml'%d_code
            content4=urllib.request.urlopen(url4).read().decode('utf-8')
            #print(content4)
            code=content4.split('|')[1]
            line="'%s':'%s',\n"%(name,code)
             
#保存文件
            result='city = {\n%s}'%line
            print (result)
            #f = open('city2.py', 'a')
            #f.write(result)
            #f.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值