鱼C论坛

 找回密码
 立即注册
查看: 3823|回复: 71

[作品展示] 爬虫爬取天气信息(天气预报)

[复制链接]
发表于 2018-8-17 00:09:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用python写了一个爬虫,爬取中国天气网上的天气信息,大家感兴趣的可以支持一下;
经验:中国天气网进行天气查询的时候,每个城市的url都是与一个城市代码相关,比如北京就是101010100,每个城市都有自己的一个代码。
这个城市及代码的对应在这里分享给大家;
想看代码的感谢大家回复支持一下(由于时间比较短,正则表达式比较暴力。)!

import re
import urllib.request
import urllib.parse


def getcitynumberlist():
    file = open("f://citynumber.txt", "r");
    listOfcity = dict({});
    while True:
        line = file.readline();
        if line != '\n':
            r_key = "([0-9]*?)=(.*?) ";
            key = re.compile(r_key);
            numberOfcity = re.findall(key, line);
            if len(numberOfcity) != 0:
                listOfcity[numberOfcity[0][1]] = numberOfcity[0][0];
        if not line:
            break;

    return listOfcity;

def getHtml(url):
    header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"};
    Request = urllib.request.Request(url, headers = header);
    response = urllib.request.urlopen(Request);
    html = response.read().decode("utf-8");

    return html;

def getweatherreport(citynumber):
    url = "http://www.weather.com.cn/weather/" + citynumber + ".shtml";
    html = getHtml(url);

    r_key ='''<ul class=\"t clearfix\">
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<i>(.*?)</i>
</p>
<p class=\"win\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>
</ul>
'''

    key = re.compile(r_key);
    list = re.findall(key, html);

    return list;






def chaxun():
    listOfcity = getcitynumberlist();

    city = str(input("please input a city:"));

    if city in listOfcity:
        weatherlist = getweatherreport(listOfcity[city]);
        print("以下是您查询城市未来七天的天气预报:");
        print("\n");
        print("----------");
        print("时间:" + weatherlist[0][0]);
        print("天气:" + weatherlist[0][1]);
        print("温度:" + weatherlist[0][2]);
        print("风向:" + weatherlist[0][3] + weatherlist[0][4]);

        for num in range(1, 7):
            judge = (num - 1) * 7 + 5;
            print("----------");
            print("时间:" + weatherlist[0][judge]);
            print("天气:" + weatherlist[0][judge + 1]);
            print("温度:" + weatherlist[0][judge + 2] + "~" + weatherlist[0][judge + 3]);
            print("风向:" + weatherlist[0][judge + 4] + "转" + weatherlist[0][judge + 5] + "  " + "风力:" + weatherlist[0][judge + 6]);


    else:
        print("对不起,未找到你所输入的城市!");



if __name__ == "__main__":
    chaxun();

在这里的第一个函数中是打开上面的城市代码文件,大家讲城市代码的txt文件保存下来后,然后open函数中的路径改为你电脑中的路径即可。
另外User Agent也按照自己的改一下就行了!
天气.JPG

citynumber.txt

44.11 KB, 下载次数: 109

天气预报城市代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-17 07:59:59 | 显示全部楼层
import re
import urllib.request
import urllib.parse


def getcitynumberlist():
    file = open("f://citynumber.txt", "r");
    listOfcity = dict({});
    while True:
        line = file.readline();
        if line != '\n':
            r_key = "([0-9]*?)=(.*?) ";
            key = re.compile(r_key);
            numberOfcity = re.findall(key, line);
            if len(numberOfcity) != 0:
                listOfcity[numberOfcity[0][1]] = numberOfcity[0][0];
        if not line:
            break;

    return listOfcity;

def getHtml(url):
    header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"};
    Request = urllib.request.Request(url, headers = header);
    response = urllib.request.urlopen(Request);
    html = response.read().decode("utf-8");

    return html;

def getweatherreport(citynumber):
    url = "http://www.weather.com.cn/weather/" + citynumber + ".shtml";
    html = getHtml(url);

    r_key ='''<li class=\".*?\">
<h1>(.*?)</h1>
<big class=\".*?\"></big>
<big class=\".*?\"></big>
<p title=\".*?\" class=\".*?\">(.*?)</p>
<p class=\".*?\">
<span>(.*?)</span>/<i>(.*?)</i>
</p>
<p class=\".*?\">
<em>
<span title=\"(.*?)\" class=\".*?\"></span>
<span title=\"(.*?)\" class=\".*?\"></span>
</em>
<i>(.*?)</i>
</p>
<div class=\".*?\"></div>
</li>''';

    key = re.compile(r_key);
    list = re.findall(key, html);

    return list;






def chaxun():
    listOfcity = getcitynumberlist();

    city = str(input("please input a city:"));

    if city in listOfcity:
        weatherlist = getweatherreport(listOfcity[city]);
        print("以下是您查询城市未来七天的天气预报:");

        for num in range(0, 7):
            print("----------");
            print("时间:" + weatherlist[num][0]);
            print("天气:" + weatherlist[num][1]);
            print("温度:" + weatherlist[num][2] + "~" + weatherlist[num][3]);
            print("风向:" + weatherlist[num][4] + "转" + weatherlist[num][5] + "  " + "风力:" + weatherlist[num][6]);


    else:
        print("对不起,未找到你所输入的城市!");



if __name__ == "__main__":
    chaxun();


不好意思大家,出了点小问题,昨天在爬的时候中国天气网提供的当日天气信息跟今天不太一样,昨天只提供了实时温度,所以第一天的信息格式与后面六天不太一样,而今天则是提供了温度范围,与后面六天完全一样,这是代码,大家根据中国天气网上的信息自己选择一下代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2018-8-17 10:45:07 | 显示全部楼层
参考下代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-17 12:14:07 | 显示全部楼层
谢谢分享,学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-17 16:57:37 From FishC Mobile | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-17 18:14:50 | 显示全部楼层
谦虚的态度来学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-17 19:18:32 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-17 20:09:41 | 显示全部楼层
嗯嗯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-17 22:45:44 | 显示全部楼层
卤煮,有些城市代码失效惹,像那些新港,呼伦贝尔,,,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-20 13:49:54 | 显示全部楼层
7666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-20 17:39:44 | 显示全部楼层
thx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-21 09:43:49 | 显示全部楼层
看一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-25 10:10:20 | 显示全部楼层
二维若
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-26 18:58:54 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-30 18:42:41 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-6 15:13:26 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-6 15:21:58 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-6 15:43:37 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-9 14:37:16 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-24 21:04:01 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表