第一步
先从网页控制台中提取出网页信息
#提取网页信息
html = """<div class="citychk">
<dl>
<dt><b style="color:Red">热门城市</b></dt>
<dd><a href="/aqi/beijing.html">北京 </a> <a href="/aqi/tianjin.html">天津 </a>
<a href="/aqi/shanghai.html">上海 </a> <a href="/aqi/chongqing.html">重庆 </a><a href="/aqi/guangzhou.html">广州 </a><a href="/aqi/shenzhen.html">深圳 </a><a href="/aqi/hangzhou.html">杭州 </a> <a href="/aqi/chengdu.html">成都 </a> <a href="/aqi/aqi_rank.html" target="_blank"><span class="aqi-lv4"><b>全国空气质量排名</b></span></a>
</dd>
</dl>
<dl>
<dt><b>河北</b></dt><dd><a href="/aqi/shijiazhuang.html">石家庄 </a><a href="/aqi/tangshan.html">唐山 </a>
<a href="/aqi/qinhuangdao.html">秦皇岛 </a><a href="/aqi/baoding.html">保定 </a><a href="/aqi/zhangjiakou.html">张家口 </a>
<a href="/aqi/handan.html">邯郸 </a><a href="/aqi/xingtai.html">邢台 </a><a href="/aqi/chengde.html">承德 </a>
<a href="/aqi/cangzhou.html">沧州 </a><a href="/aqi/langfang.html">廊坊 </a><a href="/aqi/hengshui.html">衡水 </a>
</dd>
</dl>
<dl>
<dt><b>山西</b></dt><dd><a href="/aqi/taiyuan.html">太原 </a><a href="/aqi/datong.html">大同 </a> <a href="/aqi/yangquan.html">阳泉 </a><a href="/aqi/changzhi.html">长治 </a> <a href="/aqi/linfen.html">临汾 </a>
<a href="/aqi/jincheng.html">晋城 </a> <a href="/aqi/shuozhou.html">朔州 </a> <a href="/aqi/sxyuncheng.html">运城 </a> <a href="/aqi/xinzhou.html">忻州 </a> <a href="/aqi/lvliang.html">吕梁 </a> <a href="/aqi/jinzhong.html">晋中 </a>
</dd>
</dl>
<dl>
<dt><b>内蒙古</b></dt><dd><a href="/aqi/huhehaote.html">呼和浩特 </a><a href="/aqi/baotou.html">包头 </a><a href="/aqi/eerduosi.html">鄂尔多斯 </a>
<a href="/aqi/wuhai.html">乌海 </a> <a href="/aqi/chifeng.html">赤峰 </a> <a href="/aqi/tongliao.html">通辽 </a> <a href="/aqi/bayannaoer.html">巴彦淖尔 </a><wbr> <a href="/aqi/xinganmeng.html">兴安盟 </a> <a href="/aqi/alashanmeng.html">阿拉善盟 </a> <a href="/aqi/hulunbeier.html">呼伦贝尔 </a> <a href="/aqi/erlianhaote.html">二连浩特 </a> <a href="/aqi/xilinguole.html">锡林郭勒 </a>
</dd>
</dl>
<dl>
<dt><b>辽宁</b></dt><dd><a href="/aqi/shenyang.html">沈阳 </a><a href="/aqi/dalian.html">大连 </a><a href="/aqi/dandong.html">丹东 </a><a href="/aqi/yingkou.html">营口 </a><a href="/aqi/panjin.html">盘锦 </a><a href="/aqi/huludao.html">葫芦岛 </a> <a href="/aqi/anshan.html">鞍山 </a> <a href="/aqi/jinzhou.html">锦州 </a><a href="/aqi/benxi.html">本溪 </a> <a href="/aqi/wafangdian.html">瓦房店 </a><wbr> <a href="/aqi/fushun.html">抚顺 </a> <a href="/aqi/liaoyang.html">辽阳 </a> <a href="/aqi/fuxin.html">阜新 </a> <a href="/aqi/chaoyang.html">朝阳 </a> <a href="/aqi/tieling.html">铁岭 </a>
</dd>
</dl>
<dl>
<dt><b>吉林</b></dt><dd><a href="/aqi/changchun.html">长春 </a> <a href="/aqi/jilin.html">吉林 </a> <a href="/aqi/siping.html">四平 </a> <a href="/aqi/liaoyuan.html">辽源 </a> <a href="/aqi/baishan.html">白山 </a> <a href="/aqi/songyuan.html">松原 </a> <a href="/aqi/baicheng.html">白城 </a> <a href="/aqi/yanbian.html">延边 </a> <a href="/aqi/tonghua.html">通化 </a>
</dd>
</dl>
<dl>
<dt><b>黑龙江</b></dt><dd><a href="/aqi/haerbin.html">哈尔滨 </a> <a href="/aqi/qiqihaer.html">齐齐哈尔 </a> <a href="/aqi/jixi.html">鸡西 </a> <a href="/aqi/hegang.html">鹤岗 </a> <a href="/aqi/shuangyashan.html">双鸭山 </a> <a href="/aqi/daqing.html">大庆 </a> <a href="/aqi/jiamusi.html">佳木斯 </a> <a href="/aqi/qitaihe.html">七台河 </a><wbr> <a href="/aqi/mudanjiang.html">牡丹江 </a> <a href="/aqi/heihe.html">黑河 </a> <a href="/aqi/suihua.html">绥化 </a> <a href="/aqi/daxinganling.html">大兴安岭 </a> <a href="/aqi/yichun.html">伊春 </a> <a href="/aqi/hljgannan.html">甘南 </a>
</dd>
</dl>
<dl>
<dt><b>江苏</b></dt><dd><a href="/aqi/nanjing.html">南京 </a><a href="/aqi/wuxi.html">无锡 </a><a href="/aqi/xuzhou.html">徐州 </a><a href="/aqi/changzhou.html">常州 </a>
<a href="/aqi/suzhou.html">苏州 </a><a href="/aqi/nantong.html">南通 </a><a href="/aqi/lianyungang.html">连云港 </a>
<a href="/aqi/huaian.html">淮安 </a><a href="/aqi/yancheng.html">盐城 </a><a href="/aqi/yangzhou.html">扬州 </a>
<a href="/aqi/zhenjiang.html">镇江 </a> <a href="/aqi/jstaizhou.html">泰州 </a> <a href="/aqi/suqian.html">宿迁 </a><wbr>
<a href="/aqi/kunshan.html">昆山 </a> <a href="/aqi/haimen.html">海门 </a> <a href="/aqi/taicang.html">太仓 </a> <a href="/aqi/jiangyin.html">江阴 </a> <a href="/aqi/liyang.html">溧阳 </a> <a href="/aqi/jintan.html">金坛 </a> <a href="/aqi/yixing.html">宜兴 </a><a href="/aqi/jurong.html">句容 </a><a href="/aqi/changshu.html">常熟 </a><a href="/aqi/wujiang.html">吴江 </a><a href="/aqi/zhangjiagang.html">张家港 </a>
</dd>
</dl>
<dl>
<dt><b>浙江</b></dt><dd><a href="/aqi/hangzhou.html">杭州 </a><a href="/aqi/ningbo.html">宁波 </a><a href="/aqi/wenzhou.html">温州 </a><a href="/aqi/jiaxing.html">嘉兴 </a>
<a href="/aqi/huzhou.html">湖州 </a><a href="/aqi/jinhua.html">金华 </a><a href="/aqi/quzhou.html">衢州 </a>
<a href="/aqi/zhoushan.html">舟山 </a><a href="/aqi/taizhou.html">台州 </a><a href="/aqi/lishui.html">丽水 </a><a href="/aqi/shaoxing.html">绍兴 </a><a href="/aqi/yiwu.html">义乌 </a><a href="/aqi/zjfuyang.html">富阳 </a><a href="/aqi/linan.html">临安 </a>
</dd>
</dl>
<dl>
<dt><b>安徽</b></dt><dd><a href="/aqi/hefei.html">合肥 </a> <a href="/aqi/wuhu.html">芜湖 </a> <a href="/aqi/bangbu.html">蚌埠 </a> <a href="/aqi/huainan.html">淮南 </a> <a href="/aqi/maanshan.html">马鞍山 </a> <a href="/aqi/huaibei.html">淮北 </a> <a href="/aqi/tongling.html">铜陵 </a> <a href="/aqi/anqing.html">安庆 </a> <a href="/aqi/huangshan.html">黄山 </a> <a href="/aqi/chuzhou.html">滁州 </a> <a href="/aqi/fuyang.html">阜阳 </a> <a href="/aqi/anhuisuzhou.html">宿州 </a> <wbr><a href="/aqi/chaohu.html">巢湖 </a> <a href="/aqi/liuan.html">六安 </a> <a href="/aqi/bozhou.html">亳州 </a> <a href="/aqi/chizhou.html">池州 </a> <a href="/aqi/xuancheng.html">宣城 </a>
</dd>
</dl>
<dl>
<dt><b>福建</b></dt><dd><a href="/aqi/fujianfuzhou.html">福州 </a><a href="/aqi/xiamen.html">厦门 </a><a href="/aqi/quanzhou.html">泉州 </a>
<a href="/aqi/putian.html">莆田 </a> <a href="/aqi/sanming.html">三明 </a> <a href="/aqi/zhangzhou.html">漳州 </a> <a href="/aqi/nanping.html">南平 </a> <a href="/aqi/longyan.html">龙岩 </a> <a href="/aqi/ningde.html">宁德 </a>
</dd>
</dl>
<dl>
<dt><b>江西</b></dt><dd><a href="/aqi/nanchang.html">南昌 </a> <a href="/aqi/jingdezhen.html">景德镇 </a> <a href="/aqi/pingxiang.html">萍乡 </a> <a href="/aqi/xinyu.html">新余 </a> <a href="/aqi/yingtan.html">鹰潭 </a> <a href="/aqi/ganzhou.html">赣州 </a> <a href="/aqi/jxyichun.html">宜春 </a> <a href="/aqi/fuzhou.html">抚州 </a> <a href="/aqi/jiujiang.html">九江 </a> <a href="/aqi/shangrao.html">上饶 </a> <a href="/aqi/jian.html">吉安 </a>
</dd>
</dl>
<dl>
<dt><b>山东</b></dt><dd><a href="/aqi/jinan.html">济南 </a><a href="/aqi/qingdao.html">青岛 </a><a href="/aqi/zibo.html">淄博 </a>
<a href="/aqi/zaozhuang.html">枣庄 </a><a href="/aqi/dongying.html">东营 </a><a href="/aqi/yantai.html">烟台 </a><a href="/aqi/weifang.html">潍坊 </a><a href="/aqi/sdjining.html">济宁 </a>
<a href="/aqi/taian.html">泰安 </a><a href="/aqi/weihai.html">威海 </a><a href="/aqi/rizhao.html">日照 </a>
<a href="/aqi/laiwu.html">莱芜 </a> <a href="/aqi/linyi.html">临沂 </a>
<wbr> <a href="/aqi/dezhou.html">德州 </a> <a href="/aqi/liaocheng.html">聊城 </a> <a href="/aqi/binzhou.html">滨州 </a>
<a href="/aqi/heze.html">菏泽 </a><a href="/aqi/rushan.html">乳山 </a><a href="/aqi/sdrongcheng.html">荣成 </a>
<a href="/aqi/wendeng.html">文登 </a><a href="/aqi/zhangqiu.html">章丘 </a><a href="/aqi/pingdu.html">平度 </a>
<a href="/aqi/laizhou.html">莱州 </a><a href="/aqi/sdzhaoyuan.html">招远 </a><wbr><a href="/aqi/laixi.html">莱西 </a>
<a href="/aqi/jiaozhou.html">胶州 </a><a href="/aqi/penglai.html">蓬莱 </a><a href="/aqi/jiaonan.html">胶南 </a>
<a href="/aqi/shouguang.html">寿光 </a><a href="/aqi/jimo.html">即墨 </a>
</dd>
</dl>
<dl>
<dt><b>河南</b></dt><dd><a href="/aqi/zhengzhou.html">郑州 </a> <a href="/aqi/lvyang.html">洛阳 </a> <a href="/aqi/pingdingshan.html">平顶山 </a> <a href="/aqi/hebi.html">鹤壁 </a> <a href="/aqi/jiaozuo.html">焦作 </a> <a href="/aqi/luohe.html">漯河 </a> <a href="/aqi/sanmenxia.html">三门峡 </a> <a href="/aqi/nanyang.html">南阳 </a> <a href="/aqi/shangqiu.html">商丘 </a> <a href="/aqi/xinyang.html">信阳 </a> <a href="/aqi/zhoukou.html">周口 </a> <a href="/aqi/zhumadian.html">驻马店 </a> <wbr><a href="/aqi/anyang.html">安阳 </a><a href="/aqi/kaifeng.html">开封 </a><a href="/aqi/puyang.html">濮阳 </a><a href="/aqi/xuchang.html">许昌 </a><a href="/aqi/xinxiang.html">新乡 </a>
</dd>
</dl>
<dl>
<dt><b>湖北</b></dt><dd><a href="/aqi/wuhan.html">武汉 </a> <a href="/aqi/shiyan.html">十堰 </a> <a href="/aqi/yichang.html">宜昌 </a> <a href="/aqi/ezhou.html">鄂州 </a> <a href="/aqi/jingmen.html">荆门 </a> <a href="/aqi/xiaogan.html">孝感 </a> <a href="/aqi/huanggang.html">黄冈 </a> <a href="/aqi/xianning.html">咸宁 </a> <a href="/aqi/huangshi.html">黄石 </a> <a href="/aqi/enshi.html">恩施 </a> <a href="/aqi/xiangyang.html">襄阳 </a> <a href="/aqi/suizhou.html">随州 </a> <a href="/aqi/jingzhou.html">荆州 </a>
</dd>
</dl>
<dl>
<dt><b>湖南</b></dt><dd><a href="/aqi/changsha.html">长沙 </a><a href="/aqi/zhuzhou.html">株洲 </a>
<a href="/aqi/xiangtan.html">湘潭 </a> <a href="/aqi/changde.html">常德 </a> <a href="/aqi/zhangjiajie.html">张家界 </a> <a href="/aqi/yiyang.html">益阳 </a> <a href="/aqi/chenzhou.html">郴州 </a> <a href="/aqi/yongzhou.html">永州 </a> <a href="/aqi/huaihua.html">怀化 </a> <a href="/aqi/loudi.html">娄底 </a> <a href="/aqi/shaoyang.html">邵阳 </a> <a href="/aqi/yueyang.html">岳阳 </a> <a href="/aqi/xiangxi.html">湘西 </a> <a href="/aqi/hengyang.html">衡阳 </a>
</dd>
</dl>
<dl>
<dt><b>广东</b></dt><dd><a href="/aqi/guangzhou.html">广州 </a><a href="/aqi/shaoguan.html">韶关 </a><a href="/aqi/shenzhen.html">深圳 </a><a href="/aqi/zhuhai.html">珠海 </a><a href="/aqi/shantou.html">汕头 </a>
<a href="/aqi/foshan.html">佛山 </a><a href="/aqi/jiangmen.html">江门 </a><a href="/aqi/zhaoqing.html">肇庆 </a>
<a href="/aqi/huizhou.html">惠州 </a> <a href="/aqi/heyuan.html">河源 </a> <a href="/aqi/gdqingyuan.html">清远 </a>
<a href="/aqi/dongguang.html">东莞 </a> <a href="/aqi/zhongshan.html">中山 </a>
<wbr><a href="/aqi/zhanjiang.html">湛江 </a> <a href="/aqi/maoming.html">茂名 </a> <a href="/aqi/meizhou.html">梅州 </a> <a href="/aqi/shanwei.html">汕尾 </a> <a href="/aqi/yangjiang.html">阳江 </a> <a href="/aqi/chaozhou.html">潮州 </a> <a href="/aqi/jieyang.html">揭阳 </a> <a href="/aqi/yunfu.html">云浮 </a>
</dd>
</dl>
<dl>
<dt><b>广西</b></dt><dd><a href="/aqi/nanning.html">南宁 </a><a href="/aqi/liuzhou.html">柳州 </a><a href="/aqi/beihai.html">北海 </a>
<a href="/aqi/guilin.html">桂林 </a> <a href="/aqi/wuzhou.html">梧州 </a> <a href="/aqi/fangchenggang.html">防城港 </a> <a href="/aqi/gxqinzhou.html">钦州 </a> <a href="/aqi/guigang.html">贵港 </a> <a href="/aqi/guangxiyulin.html">玉林 </a> <a href="/aqi/baise
.html">百色 </a> <a href="/aqi/hezhou.html">贺州 </a> <a href="/aqi/hechi.html">河池 </a> <a href="/aqi/laibin.html">来宾 </a> <a href="/aqi/chongzuo.html">崇左 </a>
</dd>
</dl>
<dl>
<dt><b>海南</b></dt><dd><a href="/aqi/haikou.html">海口 </a> <a href="/aqi/sanya.html">三亚 </a>
</dd>
</dl>
<dl>
<dt><b>四川</b></dt><dd><a href="/aqi/chengdu.html">成都 </a> <a href="/aqi/zigong.html">自贡 </a> <a href="/aqi/panzhihua.html">攀枝花 </a> <a href="/aqi/luzhou.html">泸州 </a> <a href="/aqi/deyang.html">德阳 </a> <a href="/aqi/mianyang.html">绵阳 </a> <a href="/aqi/guangyuan.html">广元 </a> <a href="/aqi/scsuining.html">遂宁 </a> <a href="/aqi/leshan.html">乐山 </a> <a href="/aqi/nanchong.html">南充 </a>
<a href="/aqi/meishan.html">眉山 </a><wbr> <a href="/aqi/dazhou.html">达州 </a> <a href="/aqi/yaan.html">雅安 </a> <a href="/aqi/bazhong.html">巴中 </a> <a href="/aqi/ziyang.html">资阳 </a>
<a href="/aqi/ganzi.html">甘孜 </a><a href="/aqi/neijiang.html">内江 </a><a href="/aqi/yibin.html">宜宾 </a>
<a href="/aqi/guangan.html">广安 </a><a href="/aqi/aba.html">阿坝 </a><a href="/aqi/liangshan.html">凉山 </a>
</dd>
</dl>
<dl>
<dt><b>贵州</b></dt><dd><a href="/aqi/guiyang.html">贵阳 </a> <a href="/aqi/liupanshui.html">六盘水 </a> <a href="/aqi/zunyi.html">遵义 </a> <a href="/aqi/anshun.html">安顺 </a> <a href="/aqi/bijie.html">毕节 </a> <a href="/aqi/tongren.html">铜仁 </a> <a href="/aqi/qianxinan.html">黔西南 </a> <a href="/aqi/qiannan.html">黔南 </a> <a href="/aqi/qiandongnan.html">黔东南 </a>
</dd>
</dl>
<dl>
<dt><b>云南</b></dt><dd><a href="/aqi/kunming.html">昆明 </a><a href="/aqi/yuxi.html">玉溪 </a><a href="/aqi/baoshan.html">保山 </a> <a href="/aqi/zhaotong.html">昭通 </a> <a href="/aqi/lijiang.html">丽江 </a>
<a href="/aqi/lincang.html">临沧 </a> <a href="/aqi/xishuangbanna.html">西双版纳 </a> <a href="/aqi/dehong.html">德宏 </a> <a href="/aqi/nujiang.html">怒江 </a><a href="/aqi/dali.html">大理 </a> <a href="/aqi/qujing.html">曲靖 </a> <wbr><a href="/aqi/chuxiong.html">楚雄 </a><a href="/aqi/honghe.html">红河 </a> <a href="/aqi/simao.html">思茅 </a><a href="/aqi/wenshan.html">文山 </a>
<a href="/aqi/puer.html">普洱 </a><a href="/aqi/diqing.html">迪庆 </a>
</dd>
</dl>
<dl>
<dt><b>西藏</b></dt><dd><a href="/aqi/lasa.html">拉萨 </a> <a href="/aqi/linzhi.html">林芝 </a><a href="/aqi/shannan.html">山南 </a><a href="/aqi/changdu.html">昌都 </a><a href="/aqi/rikaze.html">日喀则 </a><a href="/aqi/ali.html">阿里 </a><a href="/aqi/naqu.html">那曲 </a>
</dd>
</dl>
<dl>
<dt><b>陕西</b></dt><dd><a href="/aqi/xian.html">西安 </a><a href="/aqi/tongchuan.html">铜川 </a>
<a href="/aqi/baoji.html">宝鸡 </a><a href="/aqi/xianyang.html">咸阳 </a><a href="/aqi/weinan.html">渭南 </a>
<a href="/aqi/yanan.html">延安 </a> <a href="/aqi/hanzhong.html">汉中 </a> <a href="/aqi/yulin.html">榆林 </a> <a href="/aqi/ankang.html">安康 </a> <a href="/aqi/shanglv.html">商洛 </a>
</dd>
</dl>
<dl>
<dt><b>甘肃</b></dt><dd><a href="/aqi/lanzhou.html">兰州 </a> <a href="/aqi/jiayuguan.html">嘉峪关 </a> <a href="/aqi/tianshui.html">天水 </a> <a href="/aqi/wuwei.html">武威 </a> <a href="/aqi/zhangye.html">张掖 </a> <a href="/aqi/pingliang.html">平凉 </a> <a href="/aqi/jiuquan.html">酒泉 </a> <a href="/aqi/gsqingyang.html">庆阳 </a>
<a href="/aqi/dingxi.html">定西 </a> <a href="/aqi/gannan.html">甘南 </a> <a href="/aqi/linxia.html">临夏 </a><a href="/aqi/baiyin.html">白银 </a><a href="/aqi/jinchang.html">金昌 </a><a href="/aqi/longnan.html">陇南 </a>
</dd>
</dl>
<dl>
<dt><b>青海</b></dt><dd><a href="/aqi/xining.html">西宁 </a> <a href="/aqi/haidong.html">海东 </a> <a href="/aqi/guolv.html">果洛 </a> <a href="/aqi/haibei.html">海北 </a> <a href="/aqi/hainan.html">海南 </a> <a href="/aqi/haixi.html">海西 </a> <a href="/aqi/yushu.html">玉树 </a> <a href="/aqi/huangnan.html">黄南 </a>
</dd>
</dl>
<dl>
<dt><b>宁夏</b></dt><dd><a href="/aqi/yinchuan.html">银川 </a> <a href="/aqi/shizuishan.html">石嘴山 </a> <a href="/aqi/wuzhong.html">吴忠 </a> <a href="/aqi/nxguyuan.html">固原 </a> <a href="/aqi/zhongwei.html">中卫 </a>
</dd>
</dl>
<dl>
<dt><b>新疆</b></dt><dd><a href="/aqi/wulumuqi.html">乌鲁木齐 </a> <a href="/aqi/yili.html">伊犁哈萨克州 </a> <a href="/aqi/kelamayi.html">克拉玛依 </a> <a href="/aqi/hami.html">哈密 </a><a href="/aqi/shihezi.html">石河子 </a><a href="/aqi/hetian.html">和田 </a><a href="/aqi/wujiaqu.html">五家渠 </a> <a href="/aqi/akesu.html">阿克苏 </a> <wbr><a href="/aqi/aletai.html">阿勒泰 </a> <a href="/aqi/kashi.html">喀什 </a> <a href="/aqi/kuerle.html">库尔勒 </a> <a href="/aqi/tulufan.html">吐鲁番 </a> <a href="/aqi/tacheng.html">塔城 </a>
<a href="/aqi/xjbozhou.html">博州 </a><a href="/aqi/changji.html">昌吉 </a><a href="/aqi/kezhou.html">克州 </a>
</dd>
</dl>
</div>"""
第二步
匹配出汉字和拼音,并写入TXT文件中
import re
m = re.findall('href="/aqi/\w*.html">.{0,5} ',html)#正则表达式提取出链接
#提取汉字与拼音
def generate_code(x):
end = x.index('.')#返回‘.’的下标作为结束点
code = x[11:end]
name = x.split('>')[1].strip()#提取汉字
return name,code
city_coding = list(map(generate_code, m))
print(len(city_coding),len(set(city_coding)))#判断有无重复元素
city_coding = set(city_coding)#去重
#将结果写入TXT文件
with open('./city_coding.txt','w') as f:
for line in city_coding:
f.write('\t'.join(line) + '\n')
print('saved!')
第三步
将拼音与汉字组合成字典
def get_city_coding(file = './city_coding.txt'):
city_coding = dict()#声明字典,拼音与汉字一一对应
with open(file) as f :
for line in f.readlines():
line = line.strip()#移除字符串头尾指定的字符(默认为空格或换行符)
try:
city,coding = line.split('\t')#以分隔符为界限区分拼音和汉字
city_coding[city.strip()] = coding.strip()#加入字典
except Exception as e:
continue
return city_coding
city_coding = get_city_coding()
print(city_coding)
第四步
创建网页链接
#创建网页链接
def build_url(city_coding,year = None, month = None):
BASE = 'http://www.tianqihoubao.com/aqi/'
city_base_url = BASE + '{}.html'
city_data_base_url = BASE + '{}-{}{}.html'
if year is not None and month is not None:
month = str(month) if month >= 10 else'0' + str(month)
return city_data_base_url.format(city_coding, year, month)
else:
return city_base_url.format(city_coding)
chengdu = city_coding['成都']
print(build_url(chengdu))
print(build_url(chengdu,2020,2))
第五步
import requests
from bs4 import BeautifulSoup
hangzhou = city_coding['成都']
url = build_url(hangzhou,2020,1)
#发送请求
#get post
response = requests.get(url)#发送访问请求
#查看相关信息
print(response.status_code,response.ok)
#打印返回结果
print(response.encoding)# 返回编码格式
html = response.text
soup = BeautifulSoup(html)
#print(soup)
#网页的title
print(soup.title)
#网页的文本
#print(soup.text)
#查找属性
#data_table = soup.find_all('table')
print(len(data_table))
#print(data_table)
data_table = soup.table
# 查看下data_table内容
# print(data_table)
# data.contents 将对象下的元素都获取得到 返回List
# 第一行是表头
# 隔一行有一个\n
name_index = 1
content = data_table.contents[name_index:]
result = []
for index, c in enumerate(content[::2]):
if index == 0:
result.append(tuple(['city'] + c.text.split()))
else:
result.append(tuple([chengdu] + c.text.split()))
print(len(result), result)
第六步
保存
import csv
import os
want_city = 'chengdu'
def save_csv(file, data):
if data == None or len(data) == 1: return
if os.path.exists(file):
with open(file, 'a') as f:
writer = csv.writer(f)
writer.writerows(data[1:])
else:
with open(file, 'w') as f:
writer = csv.writer(f)
writer.writerows(data)
save_csv(f'./{want_city}.csv', result)