使用python爬取天气预报

首先,附上代码

import json
import requests
import bs4

def get_html(url):
    headers = {
        'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.104 Safari/537.36',
        'ContentType':
        'text/html; charset=utf-8',
        'Accept-Language':
        'gzip, deflate, sdch',
        'Accept-Language':
        'zh-CN,zh;q=0.8',
        'Connection':
        'keep-alive',
        }

    try:
        htmlcontent = requests.get(url, headers=headers, timeout=30)
        htmlcontent.raise_for_status()
        htmlcontent.encoding = 'utf-8'
        return htmlcontent.text
    except:
        return "请求失败!"


def get_content(url):
    weather_list = []
    html = get_html(url)
    soup = bs4.BeautifulSoup(html, 'lxml')
    content_ul = soup.find('div', class_='t').find('ul', class_='clearfix').find_all('li')
    for content in content_ul:
        try:
            weather = {}
            weather['day'] = content.find('h1').text
            weather['temperature'] = content.find('p', class_='tem').span.text + content.find('p', class_='tem').em.text
            weather_list.append(weather)
        except:
            print('查询不到')
    print(weather_list)


if __name__ == '__main__':
    url = 'http://www.weather.com.cn/weather1d/101190401.shtml'
    get_content(url)
在运行的过程中出现如下问题:


主要是没有安装lxml包,主需要在电脑终端输入pip install lxml让电脑自行安装即可,运行结果如下所示:

[{'day': '16日夜间', 'temperature': '5°C'}, {'day': '17日白天', 'temperature': '12°C'}]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值