#python #xml #rss
#python #xml #RSS-канал
Вопрос:
Я новичок, поэтому, чтобы улучшить себя, я работаю над такими вещами.
Я пытаюсь получить конкретный элемент rss / xml с его идентификатором.
Я хочу получить конкретное содержимое сообщения в блоге, используя «post-id», как я могу это сделать?
извините, если это распространенный вопрос, но я действительно не могу найти реального решения для этого.
мой код таков;
import discord
import requests
import feedparser
from bs4 import BeautifulSoup
blog_url = "https://blog.counter-strike.net/index.php/feed/"
upd_url = "https://blog.counter-strike.net/index.php/category/updates/feed/"
history_file = "history.txt"
h_file = open("history.txt", "a")
def scrap_rss(scrap_param):
article_list = []
try:
r = requests.get(scrap_param)
soup = BeautifulSoup(r.content, features='xml')
articles = soup.findAll('item')
postids = soup.findAll('post-id')
print('The scraping job succeeded: ', r.status_code)
for a in articles:
title = a.find('title').text
link = a.find('link').text
published = a.find('pubDate').text
postid = a.find('post-id').text
article = {
'title': title,
'link': link,
'published': published,
'post-id': postid
}
article_list.append(article)
with open(history_file) as f:
if postid in f.read():
print("true")
else:
print("false" postid)
h_file.write(postid "n")
#newpost = a.find(".//item/[post-id=" postid "]/name")
#newpost = postids.find(text="29701")
return print(article_list)
except Exception as e:
print('The scraping job failed. See exception: ')
print(e)
print('Starting scraping')
scrap_rss(blog_url)
print('Finished scraping')
Ответ №1:
Приведенный ниже код ищет элемент с заданным идентификатором post (31917) и извлекает pubDate
import requests
import xml.etree.ElementTree as ET
r = requests.get('https://blog.counter-strike.net/index.php/feed/')
if r.status_code == 200:
root = ET.fromstring(r.text)
item = [item for item in root.findall('.//item') if
item.find('{com-wordpress:feed-additions:1}post-id').text == '31917'][0]
print(f'published at {item.find("pubDate").text}')
выходной сигнал
published at Thu, 03 Dec 2020 22:13:08 0000