python趣味挑战之爬取天气与微博热搜并自动发给微信好友

yizhihongxing

本攻略将介绍如何使用Python爬取天气和微博热搜数据,并将数据发送给微信好友。我们将使用requests库来发送HTTP请求,使用BeautifulSoup库来解析HTML页面,以及使用itchat库来实现微信自动登录和消息发送功能。

实现Python爬取天气和微博热搜数据

以下是一个示例代码,用于实现Python爬取天气和微博热搜数据:

import requests
from bs4 import BeautifulSoup
import itchat

# 发送HTTP请求
weather_url = 'http://www.weather.com.cn/weather/101010100.shtml'
weibo_url = 'https://s.weibo.com/top/summary?cate=realtimehot'
weather_response = requests.get(weather_url)
weibo_response = requests.get(weibo_url)

# 解析HTML页面
weather_soup = BeautifulSoup(weather_response.text, 'html.parser')
weibo_soup = BeautifulSoup(weibo_response.text, 'html.parser')
weather_div = weather_soup.find('div', {'id': '7d'})
weibo_table = weibo_soup.find('table')

# 处理数据
weather_data = []
for li in weather_div.find('ul').find_all('li'):
    date = li.find('h1').text
    weather = li.find_all('p')
    temperature = weather[0].text.strip()
    weather_type = weather[1].text.strip()
    wind = weather[2].text.strip()
    weather_data.append([date, temperature, weather_type, wind])

weibo_data = []
for tr in weibo_table.find_all('tr')[1:]:
    tds = tr.find_all('td')
    rank = tds[0].text.strip()
    title = tds[1].text.strip()
    hot = tds[2].text.strip()
    weibo_data.append([rank, title, hot])

# 发送数据到微信好友
def send_message():
    # 登录微信
    itchat.auto_login(hotReload=True)
    # 发送天气数据
    weather_message = '今日天气:\n'
    for data in weather_data:
        weather_message += '日期:{},温度:{},天气:{},风力:{}\n'.format(data[0], data[1], data[2], data[3])
    itchat.send(weather_message, toUserName='filehelper')
    # 发送微博热搜数据
    weibo_message = '微博热搜:\n'
    for data in weibo_data:
        weibo_message += '排名:{},标题:{},热度:{}\n'.format(data[0], data[1], data[2])
    itchat.send(weibo_message, toUserName='filehelper')
    # 退出微信
    itchat.logout()

if __name__ == '__main__':
    send_message()

在上面的代码中,我们使用requests库发送HTTP请求,获取天气和微博热搜的HTML代码。我们使用BeautifulSoup库解析HTML页面,并找到天气和微博热搜数据所在的区域。我们使用for循环遍历区域中的每一个数据,并提取日期、温度、天气、风力、排名、标题和热度信息。我们将数据保存到一个列表中。

我们定义一个send_message函数,用于发送天气和微博热搜数据到微信好友。我们使用itchat库创建微信自动登录对象,并使用auto_login方法登录微信。我们使用itchat库的send方法发送天气和微博热搜数据到微信好友。最后,我们使用logout方法退出微信。

示例1:发送全国天气数据

以下是一个示例代码,用于发送全国天气数据:

import requests
from bs4 import BeautifulSoup
import itchat

# 发送HTTP请求
url = 'http://www.weather.com.cn/textFC/hb.shtml'
response = requests.get(url)

# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')
conMidtab = soup.find('div', {'class': 'conMidtab'})
tables = conMidtab.find_all('table')

# 处理数据
data = []
for table in tables:
    trs = table.find_all('tr')[2:]
    for tr in trs:
        tds = tr.find_all('td')
        city = tds[0].text.strip()
        weather = tds[1].text.strip()
        temperature = tds[2].text.strip()
        wind = tds[3].text.strip()
        data.append([city, weather, temperature, wind])

# 发送数据到微信好友
def send_message():
    # 登录微信
    itchat.auto_login(hotReload=True)
    # 发送天气数据
    message = '全国天气:\n'
    for d in data:
        message += '城市:{},天气:{},温度:{},风力:{}\n'.format(d[0], d[1], d[2], d[3])
    itchat.send(message, toUserName='filehelper')
    # 退出微信
    itchat.logout()

if __name__ == '__main__':
    send_message()

在上面的代码中,我们使用requests库发送HTTP请求,获取全国天气页面的HTML代码。我们使用BeautifulSoup库解析HTML页面,并找到天气数据所在的表格。我们使用for循环遍历表格中的每一行数据,并提取城市、天气、温度和风力信息。我们将数据保存到一个列表中。

我们定义一个send_message函数,用于发送全国天气数据到微信好友。我们使用itchat库创建微信自动登录对象,并使用auto_login方法登录微信。我们使用itchat库的send方法发送全国天气数据到微信好友。最后,我们使用logout方法退出微信。

示例2:发送微博热搜数据

以下是另一个示例代码,用于发送微博热搜数据:

import requests
from bs4 import BeautifulSoup
import itchat

# 发送HTTP请求
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
response = requests.get(url)

# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')

# 处理数据
data = []
for tr in table.find_all('tr')[1:]:
    tds = tr.find_all('td')
    rank = tds[0].text.strip()
    title = tds[1].text.strip()
    hot = tds[2].text.strip()
    data.append([rank, title, hot])

# 发送数据到微信好友
def send_message():
    # 登录微信
    itchat.auto_login(hotReload=True)
    # 发送微博热搜数据
    message = '微博热搜:\n'
    for d in data:
        message += '排名:{},标题:{},热度:{}\n'.format(d[0], d[1], d[2])
    itchat.send(message, toUserName='filehelper')
    # 退出微信
    itchat.logout()

if __name__ == '__main__':
    send_message()

在上面的代码中,我们使用requests库发送HTTP请求,获取微博热搜页面的HTML代码。我们使用BeautifulSoup库解析HTML页面,并找到热搜数据所在的表格。我们使用for循环遍历表格中的每一行数据,并提取排名、标题和热度信息。我们将数据保存到一个列表中。

我们定义一个send_message函数,用于发送微博热搜数据到微信好友。我们使用itchat库创建微信自动登录对象,并使用auto_login方法登录微信。我们使用itchat库的send方法发送微博热搜数据到微信好友。最后,我们使用logout方法退出微信。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python趣味挑战之爬取天气与微博热搜并自动发给微信好友 - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • python 匿名函数相关总结

    Python匿名函数相关总结 1. 什么是匿名函数 在Python中,匿名函数就是没有名字的函数,也称为lambda函数。它是一种简单的、只使用一行代码就能定义的函数,通常被用于比较简单的逻辑处理。匿名函数可以传递输入参数,并返回计算结果。 2. 匿名函数的语法 在Python中,匿名函数的语法通常类似于这样: lambda input1,input2,..…

    python 2023年5月14日
    00
  • 编译器与解释器原理

    上一章我们已经了解到,编程语言其实就是一种我们人类易于理解的程序语言。我们用这种编程语言编写的程序就称为源代码。这些源代码是通过翻译器这么个东西,被翻译成二进制指令,从而让计算机能够执行我们的指令。 那么,这其中发挥很大作用的翻译器又是怎么回事? 编译型语言与解释型语言 其实,翻译器不止一种。我们根据翻译器翻译的时机,将它分为了编译器和解释器。 相应的,编程…

    2022年10月25日
    00
  • Python实现字典依据value排序

    当然,我很乐意为您提供“Python实现字典依据value排序”的完整攻略。以下是详细步骤和示例。 Python实现字典依据value排序 在Python中,我们可以使用sorted函数和lambda表达式对字典按照value进行排序。具体步骤如下: 1. 使用lambda表达式定义排序规则 首先,我们需要使用lambda表达式定义排序规则。在这个例子中,我…

    python 2023年5月13日
    00
  • django中模板的html自动转意方法

    在Django中,模板中的HTML代码会被自动转义,以防止跨站脚本攻击(XSS)。这意味着,如果您在模板中使用HTML代码,它们将被转义为实体,而不是被解释为HTML标记。但是,有时候您可能需要在模板中使用原始的HTML代码,而不是转义后的实体。以下是详细讲解Django中模板的HTML自动转义方法的攻略,包含两个例。 示例1:使用safe过滤器 在Djan…

    python 2023年5月15日
    00
  • python被修饰的函数消失问题解决(基于wraps函数)

    Python被修饰的函数消失问题解决(基于wraps函数) 在Python中,我们经常会使用装饰器来增强函数的功能,但是使用装饰器可能会引发函数名称、文档字符串、参数列表等元信息丢失的问题。 例如,下面这个被装饰的函数丢失了元信息: def my_decorator(func): def wrapper(): """Wrappe…

    python 2023年5月14日
    00
  • 如何在 3D 图形中为函数绘制曲线 – Python

    【问题标题】:How to plot a curve for a function in a 3D graphic – Python如何在 3D 图形中为函数绘制曲线 – Python 【发布时间】:2023-04-03 23:58:01 【问题描述】: 我有这个功能: z = 0.000855995633558468*x**2 + 0.0102702516…

    Python开发 2023年4月8日
    00
  • Python:从给定的数组/列表创建树结构

    【问题标题】:Python: create tree structure from given array/listPython:从给定的数组/列表创建树结构 【发布时间】:2023-04-04 23:55:01 【问题描述】: 我遇到了一个问题。 假设我有一个给定的数组,或者 4 个单独的列表(列) P1 L1 V1 O1 P1 L1 V1 O2 P1 L…

    Python开发 2023年4月6日
    00
  • Python multiprocess pool模块报错pickling error问题解决方法分析

    在使用Python的multiprocessing.Pool模块时,有时候会遇到PicklingError的错误。这个错误通常是由于无法将对象序列化为字节流导致的。本攻略将介绍如何解决这个问题。 问题描述 在使用multiprocessing.Pool时,我们可能会遇到以下错误: PicklingError: Can’t pickle <type ‘f…

    python 2023年5月13日
    00
合作推广
合作推广
分享本页
返回顶部