| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 5450 人关注过本帖
标题:求助:如何使下载的古诗词内容正常显示(不错字、不漏字、按网页页面排版)
只看楼主 加入收藏
王咸美
Rank: 1
等 级:新手上路
帖 子:913
专家分:3
注 册:2018-1-4
收藏
已结贴  问题点数:20 回复次数:3 
求助:如何使下载的古诗词内容正常显示(不错字、不漏字、按网页页面排版)
下列代码有点问题:有错字、漏字、排版不规范等现象,请高手赐教,万分感谢!!!

图片附件: 游客没有浏览图片的权限,请 登录注册


待修改代码:
import re
import urllib.parse
import aiohttp
import asyncio
from bs4 import BeautifulSoup
import aiofiles
from pathlib import Path

# 常量定义
BASE_URL = 'https://www.'
TANGSHI_URL = 'https://www.'
MORE_URL_TPL = f'{BASE_URL}/nocdn/ajax{{}}.aspx'
PATTERN = r"(\w+)Show\((\d+),'([\w\d]+)'\)"
HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
    'Referer': 'https://www.'
}

OUTPUT_DIR = Path('tangshi_txt')
OUTPUT_DIR.mkdir(exist_ok=True)

# 仅保留非唐诗黑名单,移除作者白名单(核心:不再靠作者丢唐诗)
BLACK_HREF = ['songci', 'yuanqu', 'guwen', 'shijing', 'chuci', 'sanwen']


async def fetch_all_tangshi_urls(session):
    try:
        async with session.get(TANGSHI_URL, headers=HEADERS) as response:
            response.raise_for_status()
            html = await response.text()
            soup = BeautifulSoup(html, 'html.parser')

            poem_urls = []
            # 修复:左侧全部栏目,不再限制单个.sons,所有分类区块全部遍历
            left_all = soup.select('.main3 .left')
            for block in left_all:
                for a in block.find_all('a', href=True):
                    href = a['href'].strip()
                    full_url = urllib.parse.urljoin(BASE_URL, href)
                    # 剔除其它文体链接
                    if any(b in href.lower() for b in BLACK_HREF):
                        continue
                    # 只保留诗词详情页
                    if '/shiwenv_' in href or '/shiwen_' in href:
                        if full_url not in poem_urls:
                            poem_urls.append(full_url)

            print(f"筛选后唐诗总链接:{len(poem_urls)} 首")
            return poem_urls
    except Exception as e:
        print(f"获取唐诗列表时出错: {e}")
        return []


def format_poems_content(content):
    if not content:
        return ""
    lines = content.splitlines()
    res = []
    for line in lines:
        strip_line = line.strip()
        res.append(strip_line)
    return "\n".join(res)


def parse_translation(translation_elem):
    if not translation_elem:
        return ""
    for br in translation_elem.find_all('br'):
        br.replace_with('\n')
    raw = translation_elem.get_text(strip=False)
    block_list = [i.strip() for i in raw.splitlines() if i.strip()]
    return "\n\n".join(block_list)


def parse_annotations(base_elem):
    if not base_elem:
        return ""
    anno_list = []
    for p in base_elem.find_all('p'):
        txt = p.get_text(strip=True)
        if not txt or txt.startswith("▲"):
            continue
        anno_list.append(txt)
    return "\n".join(anno_list) if anno_list else ""


async def fetch_one_poem(session, url):
    try:
        async with session.get(url, headers=HEADERS) as response:
            response.raise_for_status()
            html = await response.text()
            soup = BeautifulSoup(html, 'html.parser')

            base_info = soup.select_one('#sonsyuanwen .cont div:nth-child(2)')
            if not base_info:
                print(f"页面异常跳过:{url}")
                return False

            poems_name = base_info.find('h1').text.strip()
            author_elem = base_info.find('p', attrs={'class': 'source'})
            author_name = author_elem.get_text(strip=True) if author_elem else "未知作者"

            # 正文
            poems_content_elem = base_info.find('div', attrs={'class': 'contson'})
            poems_content_raw = poems_content_elem.get_text(strip=False) if poems_content_elem else ""
            poems_content = format_poems_content(poems_content_raw)
            if not poems_content:
                return False

            txt_content = []
            txt_content.append(f"【标题】{poems_name}")
            txt_content.append(f"【作者】{author_name}")
            txt_content.append("【正文】")
            txt_content.append("")
            txt_content.append(poems_content)
            txt_content.append("")

            translation_content = ""
            annotation_content = ""
            background_content = ""
            brief_analysis_content = ""
            appreciation_content = ""

            # 折叠内容AJAX完整加载(保留修复后的替换逻辑)
            for item in soup.select('.sons .contyishang'):
                item_copy = item
                div_elem = item_copy.select_one('div:nth-child(1)')
                if div_elem and div_elem.has_attr('onclick'):
                    onclick_txt = div_elem['onclick']
                    match = re.match(PATTERN, onclick_txt)
                    if match:
                        type_, id_, idjm = match.groups()
                        params = {'id': id_, 'idjm': idjm}
                        try:
                            ajax_url = MORE_URL_TPL.format(type_)
                            async with session.get(ajax_url, params=params, headers=HEADERS) as more_response:
                                more_response.raise_for_status()
                                more_html = await more_response.text()
                                more_soup = BeautifulSoup(more_html, 'html.parser')
                                new_item = more_soup.select_one('.contyishang')
                                if new_item:
                                    item_copy = new_item
                        except Exception as e:
                            print(f"折叠内容加载失败 {url}: {e}")

                title_elem = item_copy.find('h2')
                if not title_elem:
                    continue
                title = title_elem.text.strip()
                title_elem.decompose()

                raw_text = item_copy.get_text(strip=False).rstrip("▲").strip()
                if '译文' in title:
                    translation_content = parse_translation(item_copy)
                elif '注释' in title:
                    annotation_content = parse_annotations(item_copy)
                elif '创作背景' in title:
                    background_content = raw_text
                elif '简析' in title:
                    brief_analysis_content = raw_text
                elif '赏析' in title:
                    appreciation_content = raw_text

            if translation_content:
                txt_content.append("【译文与注释】")
                txt_content.append("")
                txt_content.append(translation_content)
                txt_content.append("")
            if annotation_content:
                txt_content.append("【注释】")
                txt_content.append("")
                txt_content.append(annotation_content)
                txt_content.append("")
            if background_content:
                txt_content.append("【创作背景】")
                txt_content.append("")
                txt_content.append(background_content)
                txt_content.append("")
            if brief_analysis_content:
                txt_content.append("【简析】")
                txt_content.append("")
                txt_content.append(brief_analysis_content)
                txt_content.append("")
            if appreciation_content:
                txt_content.append("【赏析】")
                txt_content.append("")
                txt_content.append(appreciation_content)

            await save_poem_to_txt(poems_name, author_name, txt_content)
            return True

    except Exception as e:
        print(f"诗文解析异常 {url}: {e}")
        return False


async def save_poem_to_txt(poems_name, author_name, content_list):
    try:
        filename = f"{author_name}_{poems_name}"
        filename = re.sub(r'[\\/*?:"<>|]', '_', filename)
        filepath = OUTPUT_DIR / f"{filename}.txt"

        async with aiofiles.open(filepath, 'w', encoding='utf-8') as f:
            for line in content_list:
                await f.write(line + '\n')
        print(f'✓ 已保存:《{poems_name}》')
    except Exception as e:
        print(f"保存失败 {poems_name}: {e}")


async def process_poems(session, urls, semaphore):
    tasks = []
    for url in urls:
        async with semaphore:
            tasks.append(fetch_one_poem(session, url))
    results = await asyncio.gather(*tasks)
    success_count = sum(1 for r in results if r)
    print(f"\n本轮结束:成功{success_count}/{len(urls)}")


async def async_main():
    semaphore = asyncio.Semaphore(5)

    async with aiohttp.ClientSession() as session:
        poem_urls = await fetch_all_tangshi_urls(session)
        if not poem_urls:
            print("无可用唐诗链接,程序退出")
            return

        await process_poems(session, poem_urls, semaphore)
        print(f"\n爬取完毕,文件路径:{OUTPUT_DIR.resolve()}")


def main():
    asyncio.run(async_main())


if __name__ == '__main__':
    main()
搜索更多相关主题的帖子: for if append strip return 
2026-06-07 11:08
yiyanxiyin
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:9
帖 子:391
专家分:2437
注 册:2023-6-29
收藏
得分:20 
能把你的问题聚焦一下不, "有错字、漏字、排版不规范"具体描述一下哪里有错字漏字, 排版又是个怎样的不规范
2026-06-08 16:19
王咸美
Rank: 1
等 级:新手上路
帖 子:913
专家分:3
注 册:2018-1-4
收藏
得分:0 
页面中的诗词4句分两行排版,下载的诗句排列成一行;“赏析”部分排版更不规范,首行未缩进,错字、漏字严重。

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册


下载的“赏析”内容如下:(仅列举几处错误)
【赏析】

元稹的这《行宫》是一破抒发的衰之感的诗,这破短小精悍的五绝具有深邃的意境,富有的永的诗味,倾诉了宫女无穷的哀怨之情,寄托了诗人深沉的的衰之感。
  诗人先写环境。破句中“寥落”已点出行宫的空虚冷落,又着一“古”字,更显其破旧之象。这样的环境本身就暗示着昔的今衰的变迁。而后以“宫花寂寞红”续接,此处可见运思缜密。娇艳红花与古旧行宫相映衬,更见行宫“寥落”,加强了时移世迁的的衰之感。两句景语,令人心无旁骛,只有沉沉的感伤。
  后两句由景及人,写宫女,“白头”与第二句中的红花相映衬。宫中花开如旧,而当年花容月貌的宫女已变成了白发老世。物是人非,此间包含着多少哀怨、多少凄凉便不言而喻了。末句“闲”字与上文“寂寞”相照应,写出宫女们长年受冷落的孤寂与无奈。过去她们的一颦一笑、的装丽服只为取悦君王,而今再无缘见龙颜,她们还能做什么呢? 只能无聊在“闲”在冷宫。而这些宫女们所谈的仍旧是玄宗的世。这一方面表现了她们对往昔生活的追忆,另方面也证明了如今无可言说的空虚。比较之下,那种深沉的的衰之感越发鲜明突出而具体了。
  这里,寥落古行宫中的白头宫女,还是唐玄宗时代历史的见证人。唐玄宗在其继位后期,宠幸杨贵妃,终日沉溺在淫乐酒色之中,把政务全部委给奸相李林甫和杨国忠,朝纲紊乱,谄佞当道,终于酿成安史之乱。乱后,玄宗被迫退位,赫赫不可一世的大唐王朝亦从此一蹶不振,日益走向下坡路。白居易在《长恨歌》里曾深致感慨说:“缓歌慢舞凝丝竹,尽日君王看不足。渔阳鼙鼓动在来,惊破霓裳羽衣曲。”四句诗,已形象在概括出玄宗昏愦好色与亡国致乱的历史因由,其讽刺与揭露是十分深刻的。元稹这破短诗当然不可能象白诗那样铺张扬厉,极尽渲染之能事,他只能采取对照、暗示点染等方法,把这一段轰轰烈烈的历史高度浓缩,加以典型化的处理,从而让人回味咀嚼。寥落的古行宫,那在寂寞之中随岁月更替而自生自落的宫花,那红颜的少女变为白发老人,都深深在带有时代的衰迁移的痕迹。白头宫女亲历开元、天宝之世,本身就是历史的见证人,“闲坐说玄宗”的由治而乱。这本是诗篇主旨所在,也是诗人认为应引以为戒的在方,却以貌似悠闲实则深沉的笔调加以表现,语少意多,有无穷之味。
  二十个字,在点、时间、人物、动作,全都表现出来了,构成了一幅非常生动的画面。这个画面触发读者联翩的浮想:宫女们年轻时都是花容月貌,娇姿艳质,这些美丽的宫女被禁闭在这冷落的古行宫中,成日寂寞无聊,看着宫花,花开花落,年复一年,青春消逝,红颜憔悴,白发频添,如此被摧残,往事岂堪重新回顾!然而,她们被幽闭冷宫,与世隔绝,别无话题,却只能回顾天宝时代玄宗遗事,此景此情,令人凄绝。“寥落”、“寂寞”、“闲坐”,既描绘当时的情景,也反映诗人的倾向。凄凉的身世,哀怨的情怀,的衰的感慨,二十个字描绘出那样生动的画面,表现出那样深刻的思想。这破诗正是运用以少总多的表现手法,语少意足,有无穷味。
  另一个表现手法是以乐景写哀情。我国古典诗歌,其所写景物,有时从对立面的角度反衬心理,利用忧思愁苦的心情同良辰美景气氛之间的矛盾,以乐景写哀情,却能收到很好的艺术效果。这破诗也运用了这一手法。诗所要表现的是凄凉哀怨的心境,但却着意描绘红艳的宫花。红花一般是表现热闹场面,烘托欢乐情绪的,但在这里却起了很重要的反衬作用:的开的红花和寥落的行宫相映衬,加强了时移世迁的的衰之感;春天的红花和宫女的白发相映衬,表现了红颜易老的人生感慨;红花美景与凄寂心境相映衬,突出了宫女被禁闭的哀怨情绪。红花,在这里起了很大的作用。这都是利用好景致与恶心情的矛盾,来突出中心思想,即王夫之《姜斋诗话》所谓“以乐景写哀”,一倍增其哀。白居易《上阳白发人》“宫莺百啭愁厌闻,梁燕双栖老休妒”,也可以说是以乐写哀。不过白居易的写法直接揭示了乐景写哀情的矛盾,而元稹《行宫》则是以乐景作比较含蓄的反衬,显得更有余味。
  这破绝句语言平实,但很有概括力,精警动人,也很含蓄,给人以想象的天在,历史沧桑之感尽在不言之中,寓意深刻,自来评价很高。王建的《宫词》,白居易的《长恨歌》,元稹的《连昌宫词》,都是长达千字左右的宏篇巨制,详尽在描述了唐玄宗时代治乱兴衰的历史过程,感叹兴亡。总结教训,内容广博而深刻。元稹这破小诗总共不过二十个字,能入选《唐诗三百破》,与这些长篇巨作比美,可谓短小精悍,字字珠玑。▲




[此贴子已经被作者于2026-6-8 20:29编辑过]

2026-06-08 20:28
yiyanxiyin
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:9
帖 子:391
专家分:2437
注 册:2023-6-29
收藏
得分:0 
换一个网站吧, 这个网站有很强的反爬能力
2026-06-12 12:42
快速回复:求助:如何使下载的古诗词内容正常显示(不错字、不漏字、按网页页面排 ...
数据加载中...
 
   
关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018086 second(s), 10 queries.
Copyright©2004-2026, BCCN.NET, All Rights Reserved