悠悠楠杉
网站页面
titlematch = re.search(r"标题: (.*)", text)
keywordsmatch = re.search(r"关键词: (.)", text)
description_match = re.search(r"描述: (.)", text)
text_content = re.search(r"正文: (.*)", text)
title = titlematch.group(1) if titlematch else "无标题"
keywords = keywordsmatch.group(1) if keywordsmatch else "无关键词"
description = descriptionmatch.group(1) if descriptionmatch else "无描述"
content = textcontent.group(1) if textcontent else "无正文"
{description}
{content}"""
print(markdown_text)
```
re.search(r"标题: (.*)", text)
:查找并捕获“标题:”后的所有内容。re.search(r"关键词: (.*)", text)
:查找并捕获“关键词:”后的所有内容。re.search(r"描述: (.*)", text)
:查找并捕获“描述:”后的所有内容。re.search(r"正文: (.*)", text)
:查找并捕获“正文:”后的所有内容。每个捕获组使用if
语句确保内容存在时才使用,否则用默认值“无标题”等代替。