吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1786|回复: 21
收起左侧

[Python 原创] 微博热搜公众号推送

[复制链接]
没有结果lin 发表于 2024-9-4 12:15
[Python] 纯文本查看 复制代码
import csv
import requests
import logging
import json
from datetime import datetime
import time

# Initialize logging
logging.basicConfig(level=logging.INFO)

def fetch_hot_searches():
    url = "https://weibo.com/ajax/statuses/hot_band"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62'
    }
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # Raises HTTPError for bad responses
        data = response.json()["data"]["band_list"]
        hot = []
        count = 0
        for item in data:
            count += 1
            note = item.get('note', 'Unknown')
            category = item.get('category', 'Unknown')
            raw_hot = item.get('raw_hot', 'Unknown')
            hot_url = f'https://s.weibo.com/weibo?q={note}&Refer=index'
            onboard_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            hot.append([count, note, category, raw_hot, hot_url, onboard_time])
        with open('weibo_hot.csv', 'w', newline='', encoding="utf-8") as file:
            writer = csv.writer(file)
            writer.writerow(["id", "note", "category", "raw_hot", "url"])
            # 遍历列表中的每个元素,写入 CSV 文件的单独一行中
            for item in hot:
                writer.writerow(item, )

        send_msg("热搜实时榜单", str(hot).replace("[","").replace("]","").replace("'",""))
    
    except requests.RequestException as e:
        logging.error(f"Request failed: {e}")
    except KeyError as e:
        logging.error(f"Key error: {e}")
    except json.JSONDecodeError as e:
        logging.error(f"JSON decode error: {e}")

def send_msg(head, body):
    logging.info('----------start send_msg')
    url = 'http://push.ijingniu.cn/send'
    data = {
        'key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',  # Replace with your actual key
        'head': head,
        'body': body
    }
    try:
        response = requests.post(url=url, data=data)
        response.raise_for_status()
        logging.info(response.text)
        if response.ok:
            return json.loads(response.text)['status']
        else:
            return False
    except requests.RequestException as e:
        logging.error(f"Request failed: {e}")
        return False
    except json.JSONDecodeError as e:
        logging.error(f"JSON decode error: {e}")
        return False

while True:
    fetch_hot_searches()
    time.sleep(3600)  # 1个小时推送一次

#fetch_hot_searches()

上面代码替换自己的秘钥

image.png

image.png


ebbb833c111cc09ad6de04f91093618.jpg

免费评分

参与人数 3吾爱币 +7 热心值 +3 收起 理由
xnink + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
tenvten + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wudavid33 发表于 2024-9-4 13:49
哇,这个厉害,谢谢分享
fengbaby2003 发表于 2024-9-4 13:52
1610453925 发表于 2024-9-4 14:14
cloverXR 发表于 2024-9-4 14:19

厉害,这个不错
anf 发表于 2024-9-4 14:23
拿过来 看看
xfmiao 发表于 2024-9-4 14:44
太厉害;
hfhskf2005 发表于 2024-9-4 15:15
厉害啦,期待成品
yizhiyuanmo007 发表于 2024-9-4 15:18
直接扣6666
qwqwqw9999 发表于 2024-9-4 15:33
这个厉害了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-28 07:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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