当前位置: 首页 > news >正文

网页如何集成各社区征文活动

Helllo , 我是小恒
由于我需要腾讯云社区,稀土掘金以及CSDN的征文活动RSS,找了一下没发现,所以使用GET
请求接口对网页定时进行拉取清洗,甚至无意间做了一个简单的json格式API

最终网址:hub.liheng.work
API:http://hub.liheng.work/activities.json
GitHub:https://github.com/lmliheng/hub
在这里插入图片描述

原理

由于浏览器的同源策略产生的跨域问题,使得CSDN官方URL无法被请求获取展示到前端
使用后端代码GET网页代码,对其进行数据清洗,并导入json文件
注意后端程序的定时任务以及日志打印
前端代码调用本地json,也不存在跨域,从而实现需求

代码结构

├───pyproject/
│   ├───activities.json
│   ├───htmlone.py
│   ├───index.html
│   ├───script.log

后端

实现HTML转json的数据清洗,以及打印日志到scripts.log文件

#作者:小恒不会java
#时间:2024年5月13日
#微信:a13551458597
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import json
import logging
from datetime import datetimelogging.basicConfig(filename='script.log', level=logging.INFO)
logging.info('Script started at {}'.format(datetime.now()))# 获取HTML内容,这种形式是避免get请求的跨域问题
url = 'https://bbs.csdn.net/forums/activity?spm=1035.2022.3001.8781&typeId=745490'
response = requests.get(url)
html_content = response.text# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')activities = []# 检查做到避免重复活动
posts = soup.find_all('div', {'class': 'content'})
for post in posts:activity = {}# 获取活动名称title_element = post.find('div', {'class': 'long-text-title'})if title_element:activity['name'] = title_element.text.strip()# 获取活动简介desc_element = post.find('div', {'class': 'item-desc'})if desc_element:activity['description'] = desc_element.text.strip()# 获取活动链接link_element = post.find('a', href=True)if link_element:activity['link'] = link_element['href']# 检查活动是否已存在if 'link' in activity and not any(existing_activity['link'] == activity['link'] for existing_activity in activities):activities.append(activity)print(activities)with open('activities.json', 'w', encoding='utf-8') as f:json.dump(activities, f, ensure_ascii=False, indent=4)logging.info('Script finished at {}'.format(datetime.now()))

定时任务

我服务器系统是linux centos7
使用cron完成定时运行,并通过python代码日志打印检验运行情况

检查cron服务是否正在运行:
```shell
sudo systemctl status cron或者ceond

如果cron服务未运行,请使用以下命令启动它:

sudo systemctl start cron

编辑crontab文件

crontab -e

在打开的编辑器中,添加一行以设置定时任务。例如,要每天凌晨1点运行Python脚本,请添加以下行

0 1 * * * /usr/bin/python /path/to/your/script.py

列出当前用户的crontab条目:

crontab -l

日志打印检查

scripts.log

[root@iZ7xvavc793m36sybr4bw4Z hub.liheng.work]# cat scripts.log
INFO:root:Script started at 2024-05-13 21:11:36.571745
INFO:root:Script finished at 2024-05-13 21:11:37.311995
[root@iZ7xvavc793m36sybr4bw4Z hub.liheng.work]# 
http://www.lryc.cn/news/348245.html

相关文章:

  • 【知识碎片】2024_05_13
  • Day53代码随想录动态规划part13:300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
  • 自己动手为wordpress注册一个Carousel轮播区块
  • 基于Springboot的实习生管理系统(有报告)。Javaee项目,springboot项目。
  • 良心实用的电脑桌面便利贴,好用的便利贴便签小工具
  • Eayswoole 报错 crontab info is abnormal
  • 移动 App 入侵与逆向破解技术-iOS 篇
  • 2024服贸会,参展企业媒体宣传报道攻略
  • CI/CD笔记.Gitlab系列.新用户管理
  • 前端 JS 经典:JS 基础类型和 typeof
  • Java入门基础学习笔记11——关键字和标识符
  • 设计模式-解释器模式(Interpreter)
  • 机器视觉任务中语义分割方法的进化历史
  • Java并发编程: Synchronized锁升级
  • Atcoder C - Routing
  • 升级! 测试萌新Python学习之连通数据库Pymsql增删改及封装(四)
  • 【大数据】containered学习笔记
  • 「TypeScript」TypeScript入门练手题
  • k8s 使用Docker和Containerd对比分析
  • MySQL 通过 systemd 启动时 hang 住了……
  • pat乙1033-旧键盘打字
  • Ubuntu安装VScode
  • c# - - - winform程序四个角添加圆角效果
  • Springboot 集成 Consul 实现服务注册中心-05
  • 【软考高项】四十六、项目管理科学计算之运筹学
  • 使用 Python 和 OpenCV 进行实时目标检测的详解
  • Android build.prop生成过程源码分析
  • 计算机网络教材——谢希仁教材与配套PPT课件和《计算机网络——自顶向下方法》
  • mysql 离线安装
  • 【C++】 string类:应用与实践