Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/mcbbs_news/mcbbs_news.py

45 lines
1.5 KiB
Python
Raw Normal View History

import datetime
2022-01-20 12:13:03 +00:00
2022-07-31 08:27:58 +00:00
from bs4 import BeautifulSoup
2022-01-19 14:41:31 +00:00
from config import Config
from core.elements import Url
from core.utils import get_url
async def news():
api = 'https://www.mcbbs.net/forum-news-1.html'
webrender = Config('web_render')
if webrender:
api = webrender + 'source?url=' + api
2022-01-19 14:41:31 +00:00
html = await get_url(api)
print(html)
bs = BeautifulSoup(html, 'html.parser')
results = bs.select('#threadlisttableid > tbody[id^="normalthread_"]')
res = []
if results is not None:
for i in results:
if len(res) == 5:
break
a = i.select_one('a.s.xst')
if not a.has_attr('style'):
continue
category = i.select_one('tr > th > em > a').get_text()
author = i.select_one(
'tr > td.by > cite > a').get_text()
time = i.select_one('tr > td.by > em').get_text()
if time.find('-') != -1:
time = time.split('-')
time_class = datetime.date(
int(time[0]), int(time[1]), int(time[2]))
delta = datetime.date.today() - time_class
time = str(delta.days) + ' 天前'
2022-01-19 14:41:31 +00:00
title = a.get_text()
url = Url('https://www.mcbbs.net/' + a.get('href'))
res += [{'count': len(res) + 1, 'title': title, 'url': url,
'author': author, 'time': time, 'category': category}]
2022-01-19 14:41:31 +00:00
return res
else:
return None