Python爬虫入门免费获取天气信息

三人行必有我师。大家可以对这篇文章提出一些建议,努力改进,共同学习。

虽然今年的计划是考研,但是不敲代码对我来说手痒痒,做了一个爬虫,分享给大家共同学习。

目录

1.信息爬取:

2.代码实现获取相应信息 

附上代码: 

毛遂自荐:


 

1.信息爬取:

      1.找到一个实时更新且不需要花钱的天气网站(企鹅天气),我们第一步需要判断这个网站是否像“白话文一样好理解”,意思就是他是否会在网页源代码中直接提供给你你可以看到的信息。我们在网页上右键->查看网页源代码

结果我们发现,他并不想直接告诉我们他的数据就放在源代码里,而需要我们去深入探索

       2. 第一步,我们发现查找源代码的方法并不能得到我们所需要的数据,那我们接下来看一下网络上是怎么交互的吧。我们在网页上右键-->检查 ,或者可以按F12,不同的浏览器启动的方式可能不太一样(博主的浏览器是Chrome)。

企鹅天气

     3.执行过第二步之后,我们选择Network,并且刷新当前网页,我们可以发现会有很多信息弹出。其实这些就是服务器和浏览器之间的交互,通过这个功能我们可以清晰的获取到他们交互的内容是什么。是否会有一些见不得人的东西(程序员的牢骚),大家都可以去探索(⊙o⊙)哦

      4.废话不多说, 接下来我们分析这些文件到底是什么,我们一步步的在密集的文字中查找带有敏感词汇(ip,common等等)的文件,就能很快定位到我们所需要的信息。

我根据敏感词汇找到了如下文件,往后一拉,发现这不正是我们所需要的信息吗,有地址,温度,网页上你所能看到的数值都在这个文件中。

5.但是我们还不能高兴的太早,我们得判断这个网址是否有效,就需要我们在浏览器中打开这个网址。 

1.复制网址->打开新的标签页打开网页,或者直接右键->转到(后者比较准确,不像我们人为还会出错)

2. 我们更改link中的

city = 徐州市
province = 江苏省

刷新过后,发现内容和(企鹅天气)信息对应,没错了,这个网站就是我们需要的网站。

如果大家觉得这样子看着很难受,我们可以Ctrl-a,Ctrl-c,全文复制到在线json解析网站进行解析 推荐-->json,这样我们就能清晰,有条理的进行json获取信息了。

2.代码实现获取相应信息 

      1.同样,我们复制刚刚获取到的网址。

#https://wis.qq.com/weather/common?source=pc&weather_type=observe%7Cforecast_1h%7Cforecast_24h%7Cindex%7Calarm%7Climit%7Ctips%7Crise&province=江苏省&city=徐州市

我们所用到的库有:

import requests  # http请求库
import re        # 正则表达式库
import json      # json处理库

      2.4行代码实现获取Response,其实只需要两行代码,我这里保存到本地了 。

now_weather = requests.get("https://wis.qq.com/weather/common?source=pc&weather_type=observe%7Cforecast_1h%7Cforecast_24h%7Cindex%7Calarm%7Climit%7Ctips%7Crise&province="+省会+"&city="+城市
    with open('weather.txt','w+') as f:
        f.write(now_weather)
    f.close()

      3.通过json搜索任何一个想要的数据。不管那个想要的数据在哪,都能给他揪出来。

我们要获取的是:data-->observe下的数据

操作很简单,其余数据都能这么获取,其实更高级的可以使用for循环实现 。

def get_all_informations():
    get_now_weather()
    with open("weather.txt") as f:
        information = f.read()
    f.close()
    json_information = json.loads(information) #解析成json格式
    if json_information['status']!=200:        #判断数据获取是否通畅
        print("数据源解析错误,请检查数据源是否正确!")
    else:
        degree = json_information['data']['observe']['degree']                #温度
        humidity = json_information['data']['observe']['humidity']            #湿度
        pressure = json_information['data']['observe']['pressure']            #气压
        weather = json_information['data']['observe']['weather']              #天气状况
        weather_code = json_information['data']['observe']['weather_code']    #天气代码,在网页中起着选择图片的功能
        wind_direction = json_information['data']['observe']['wind_direction']#风向
        wind_power = json_information['data']['observe']['wind_power']        #凤等级
        tips = json_information['data']['tips']['observe']['0']               #凤等级

附上代码: 

import requests
import re
import json
# 获取当前的位置和ip地址
def get_ipaddress_location():
    r = requests.get('http://ip.tool.chinaz.com/').text
    ip = re.findall("<dd class=\"fz24\">(.*?)</dd>", r)
    location = re.findall("<dd>(.*?)<a href=\"", r)
    return location, ip

def get_now_weather():
    locations = ['','']
    location, ip = get_ipaddress_location()
    index = location[0].index('省');index2 = location[0].index('市')
    locations[0] = location[0][:index+1];locations[1] = location[0][index+1:index2+1]
    now_weather = requests.get("https://wis.qq.com/weather/common?source=pc&weather_type=observe%7Cforecast_1h%7Cforecast_24h%7Cindex%7Calarm%7Climit%7Ctips%7Crise&province="+locations[0]+"&city="+locations[1]).text
    with open('weather.txt','w+') as f:
        print(now_weather)
        f.write(now_weather)
    f.close()

def get_all_informations():
    get_now_weather()
    with open("weather.txt") as f:
        information = f.read()
    f.close()
    json_information = json.loads(information) #解析成json格式
    if json_information['status']!=200:        #判断数据获取是否通畅
        print("数据源解析错误,请检查数据源是否正确!")
    else:
        degree = json_information['data']['observe']['degree']                #温度
        humidity = json_information['data']['observe']['humidity']            #湿度
        pressure = json_information['data']['observe']['pressure']            #气压
        weather = json_information['data']['observe']['weather']              #天气状况
        weather_code = json_information['data']['observe']['weather_code']    #天气代码,在网页中起着选择图片的功能
        wind_direction = json_information['data']['observe']['wind_direction']#风向
        wind_power = json_information['data']['observe']['wind_power']        #凤等级
        tips = json_information['data']['tips']['observe']['0']               #凤等级
    print("温度:"+degree)

毛遂自荐:

大家可以多多支持我的其他博客。

《疫情让我使用V-rep仿真(结合pythonAPI)实现机器人视觉巡线+pid调速》

我的个人博客:锡城小凯的博客

你们的支持就是对我的鼓励~~

发布了24 篇原创文章 · 获赞 48 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xiaokai1999/article/details/105475822