Python爬取中国天气网实时气温数据

小程序目标

使用Python简单编写一个爬虫,爬取中国天气网的气温数据。

前期的一些尝试

requests + re

使用正则表达式匹配requests返回的数据,初学过程中借鉴了网上大神的一段代码:https://www.cnblogs.com/Rhythm-/p/9255255.html

import requests
import re


def get_weather(url):
    response = requests.get(url)
    response.encoding = 'utf-8'

	# 抓取当天气温(非实时)
    aim = re.findall('<input type="hidden" id="hidden_title" value="(.*?)月(.*?)日(.*?)时 (.*?)  (.*?)  (.*?)"',
                     response.text, re.S)
    print("今日气温:%s" % aim[0][5])


if __name__ == "__main__":
    url_bj = "http://www.weather.com.cn/weather1d/101010100.shtml"
    get_weather(url_bj)

输出如下:

今日气温:14/2°C

该段代码获取了下图中标红的标签:
在这里插入图片描述
可以看到,该值为 “当天的最高气温与最低气温” 与实时气温并不相同。

requests + bs4

先找到 实时气温 的标签:
在这里插入图片描述
使用bs4,创建一个BeautifulSoup对象,再使用find_all方法去搜索标签及内容:

from bs4 import BeautifulSoup
import requests


def real_time_weather(url):
	response = requests.get(url)
    response.encoding = 'utf-8'
    html = BeautifulSoup(response.text, "html.parser")
    tem = html.find_all("div", class_="tem")
    print(tem)


if __name__ == "__main__":
    url_bj = "http://www.weather.com.cn/weather1d/101010100.shtml"
    real_time_weather(url_bj)

执行程序输出如下:

[<div class="tem">
</div>]

可以看到,我们找到了标签,但是并没有输出标签中的内容。
之后在网上查找原因,从表象上看,大概是因为中国天气网使用的是shtml,造成有些内容使用requests或者urllib不可显。具体的原理我还没有查。

使用selenium爬取shtml内容

selenium会通过打开浏览器获取代码。安装selenium过程不再赘述,通过pycharm或者pip都可以安装。
因为会有打开浏览器的过程,所以该方法会显得比较耗时,后面我会再寻找其他的方法尝试。

selenium + bs4

from bs4 import BeautifulSoup
from selenium import webdriver


def real_time_weather(url):
	
	browser = webdriver.Chrome()
    browser.get(url)
    content = browser.page_source
    browser.close()
    
	html = BeautifulSoup(content, "html.parser")
	tem = html.find_all("div", class_="tem")
	# 经检查find_all方法返回的tem第一组数据为想要获取的数据
	# span区域为实时气温的数值,em区域为实时气温的单位
    result = tem[0].span.text + tem[0].em.text

    print("实时气温:" + result)


if __name__ == "__main__":
    url_bj = "http://www.weather.com.cn/weather1d/101010100.shtml"
    real_time_weather(url_bj)

执行后返回结果如下:

实时气温:4

浏览器驱动问题

在使用selenium的过程中,需要加载选用浏览器的驱动(本人使用的是chrome),这些需要我们提前下载好。否则调用的过程中会抛出如下异常:

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

浏览器驱动部署过程(以本人chrome为例):

  1. 检查chrome版本
    在这里插入图片描述
  2. 下载chromedriver
    http://chromedriver.storage.googleapis.com/index.html
    在这里插入图片描述
    在这里插入图片描述
  3. 解压后,将chromedrive.exe放入chrome的安装路径下
    在这里插入图片描述
  4. 设置环境变量,将chrome的路径加到Path变量中。
    在这里插入图片描述

配置成功后,执行程序会自动打开chrome浏览器并有下图的提示:
在这里插入图片描述

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值