我如何从JSON响应中获取数据并将其移动到一个字符串中以制作rss提要?



所以在过去的几天里,我一直在编写这段代码,为一个没有使用公共api的网站创建RSS提要。我一直有问题,试图创建提要,因为我不确定什么正确的方式来获取数据,并成功地生成提要。

下面是澄清代码:

import requests
import json
import os

def get_latest():
link = "https://api.comick.app/chapter/?page=1&order=new&accept_mature_content=true"
response = requests.get(
link,
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
},
)
if response.status_code != 200:
raise Exception(
f"Error getting data from {link}. Status code: {response.status_code}"
)
data = json.loads(response.text)
chapters = {}
for item in data:
if "md_comics" in item and "title" in item:
title = item.get("title")
chapter = item.get("chap")
md_comic = item["md_comics"]
if "slug" in md_comic:
slug = md_comic["slug"]
chapters[f"{slug}-{chapter}"] = {
"title": title,
"slug": slug,
"chapter": chapter,
}
return chapters

def generate_rss(chapters):
rss = f"""
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>ComicK - RSS Feed</title>
<link>https://github.com/ld3z/manga-rss</link>
<description>A simple RSS feed for ComicK!</description>
"""
for key, value in chapters.items():
rss += """
<item>
<title>{}</title>
<link>{}</link>
<description>{} by {}</description>
</item>
""".format(
f"{value['title']} - Chapter {value['chapter']}",
"https://comick.app/comic/{}".format(value["slug"]),
f"Chapter {value['chapter']} of {value['title']} is now available on ComicK!",
)
rss += "n</channel>n</rss>"
return rss

try:
chapters = get_latest()
rss = generate_rss(chapters)
filename = f"./comick/comick-rss.xml"
os.makedirs(os.path.dirname(filename), exist_ok=True)
if os.path.exists(filename):
os.remove(filename)
with open(filename, "w") as f:
f.write(rss.strip())
except Exception as e:
print(f"Comick failed: {e}")

如有任何帮助,不胜感激。

到目前为止,我已经尝试将放入列表、字典和其他类型中,并且不断出现类型错误的TypeError,直到我弄清楚问题是什么。现在脚本创建了文件,但文件中没有任何项,因为脚本(我相信)实际上并没有把数据放在任何地方。

尝试:

import requests

def generate_rss(data):
rss = """
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>ComicK - RSS Feed</title>
<link>https://github.com/ld3z/manga-rss</link>
<description>A simple RSS feed for ComicK!</description>
"""
for i in data:
c = i['md_comics']
rss += """
<item>
<title>{}</title>
<link>{}</link>
<description>{}</description>
</item>
""".format(
f"{c['title']} - Chapter {i['chap']}",
f"https://comick.app/comic/{c['slug']}",
f"Chapter {i['chap']} of {c['title']} is now available on ComicK!",
)
rss += "n</channel>n</rss>"
return rss
url = "https://api.comick.app/chapter/?page=1&order=new&accept_mature_content=true"
data = requests.get(url).json()

with open('rss.xml', 'w') as f_out:
print(generate_rss(data), file=f_out)

这将生成内容为

rss.xml
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>ComicK - RSS Feed</title>
<link>https://github.com/ld3z/manga-rss</link>
<description>A simple RSS feed for ComicK!</description>
<item>
<title>Hongyue Start - Chapter 46</title>
<link>https://comick.app/comic/since-the-red-moon-appeared</link>
<description>Chapter 46 of Hongyue Start is now available on ComicK!</description>
</item>
<item>
<title>Black Lagoon - Chapter 114</title>
<link>https://comick.app/comic/black-lagoon</link>
<description>Chapter 114 of Black Lagoon is now available on ComicK!</description>
</item>
<item>
<title>Darkness of the Sea, Shadow of the Moon - Chapter 88</title>
<link>https://comick.app/comic/umi-no-yami-tsuki-no-kage</link>
<description>Chapter 88 of Darkness of the Sea, Shadow of the Moon is now available on ComicK!</description>
</item>
...and so on.

相关内容

最新更新