当前位置: 首页 > article >正文

Python爬虫使用示例-古诗词摘录

一、分析需求

目标地址:

https://www.sou-yun.cn/Query.aspx?type=poem&id=×××××

在这里插入图片描述


在这里插入图片描述

二、提取诗句

import os
import re
import requests
import parsel

#url ='https://www.sou-yun.cn/PoemIndex.aspx?dynasty=Tang&author=14976&type=Jie'
url='https://www.sou-yun.cn/Query.aspx?type=poem1&id=36647'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
response = requests.get(url=url,headers=headers)
html_content= response.text
#print(response.text)
# 正则表达式匹配
poem_sentences = re.findall(r"<div class='poemSentence'[^>]*>(.*?)<\/div>", html_content, re.DOTALL)

# 清理并输出提取的诗句
for sentence in poem_sentences:
    # 移除HTML标签
    clean_sentence = re.sub(r"<.*?>", "", sentence).strip()
    if clean_sentence:  # 过滤掉空句
        print(clean_sentence)

三、其他信息

提取all需要信息,title+author+sentences


import os
import re
import requests
import parsel

#url ='https://www.sou-yun.cn/PoemIndex.aspx?dynasty=Tang&author=14976&type=Jie'
url='https://www.sou-yun.cn/Query.aspx?type=poem1&id=36647'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
response = requests.get(url=url,headers=headers)
html_content= response.text
#print(response.text)
# 提取标题
title_match = re.search(r"<span class='bold'><span class='wordLink'[^>]*>(.*?)<\/span><\/span>\s*<span[^>]*>(.*?)<\/span>\s*<span class='poemAuthor'[^>]*>(.*?)<\/span>", html_content)
if title_match:
    title = title_match.group(1) + title_match.group(2)  # 合并标题部分
    author = re.sub(r"<.*?>", "", title_match.group(3)).strip()  # 处理作者

# 正则表达式匹配诗句
poem_sentences = re.findall(r"<div class='poemSentence'[^>]*>(.*?)<\/div>", html_content, re.DOTALL)

# 清理并输出提取的信息
print("标题:", title)
print("作者:", author)
print("诗句:")

for sentence in poem_sentences:
    # 移除HTML标签
    clean_sentence = re.sub(r"<.*?>", "", sentence).strip()
    if clean_sentence:  # 过滤掉空句
        print(clean_sentence)

在这里插入图片描述
微调格式

import os
import re
import requests
import parsel

#url ='https://www.sou-yun.cn/PoemIndex.aspx?dynasty=Tang&author=14976&type=Jie'
url='https://www.sou-yun.cn/Query.aspx?type=poem1&id=36647'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
response = requests.get(url=url,headers=headers)
html_content= response.text
#print(response.text)
# 提取标题
title_match = re.search(r"<span class='bold'><span class='wordLink'[^>]*>(.*?)<\/span><\/span>\s*<span[^>]*>(.*?)<\/span>\s*<span class='poemAuthor'[^>]*>(.*?)<\/span>", html_content)
if title_match:
    title = title_match.group(1) + title_match.group(2)  # 合并标题部分
    author = re.sub(r"<.*?>", "", title_match.group(3)).strip()  # 处理作者

# 正则表达式匹配诗句
poem_sentences = re.findall(r"<div class='poemSentence'[^>]*>(.*?)<\/div>", html_content, re.DOTALL)

# 清理并输出提取的信息
print("《 " + title + "》 ("+ author + ")")
#print("作者:", author)
#print("诗句:")

for sentence in poem_sentences:
    # 移除HTML标签
    clean_sentence = re.sub(r"<.*?>", "", sentence).strip()
    if clean_sentence:  # 过滤掉空句
        print(clean_sentence)

四、保存文档

保存到txt里面,单首诗歌

import os
import re
import requests
import parsel


#url ='https://www.sou-yun.cn/PoemIndex.aspx?dynasty=Tang&author=14976&type=Jie'
url='https://www.sou-yun.cn/Query.aspx?type=poem1&id=36647'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
response = requests.get(url=url,headers=headers)
html_content= response.text
#print(response.text)
# 提取标题
title_match = re.search(r"<span class='bold'><span class='wordLink'[^>]*>(.*?)<\/span><\/span>\s*<span[^>]*>(.*?)<\/span>\s*<span class='poemAuthor'[^>]*>(.*?)<\/span>", html_content)
if title_match:
    title = title_match.group(1) + title_match.group(2)  # 合并标题部分
    author = re.sub(r"<.*?>", "", title_match.group(3)).strip()  # 处理作者

# 正则表达式匹配诗句
poem_sentences = re.findall(r"<div class='poemSentence'[^>]*>(.*?)<\/div>", html_content, re.DOTALL)

# 清理并准备写入文件的内容
output = f"《 " + title + "》 ("+ author + ")\n"
print("《 " + title + "》 ("+ author + ")")

for sentence in poem_sentences:
    # 移除HTML标签
    clean_sentence = re.sub(r"<.*?>", "", sentence).strip()
    if clean_sentence:  # 过滤掉空句
        output += clean_sentence + "\n"
        print(clean_sentence)

# 将结果写入文本文件

with open('poem.txt', 'w', encoding='utf-8') as file:
    file.write(output)

print("信息已保存到 poem.txt")

五、多首继续

不一定是符合要求的,因为这个id暂时得不到(内容结构问题)
在这里插入图片描述


找不到,因为是按照第一个写的的正则

import os
import re
import requests
import parsel


#url ='https://www.sou-yun.cn/PoemIndex.aspx?dynasty=Tang&author=14976&type=Jie'
#url='https://www.sou-yun.cn/Query.aspx?type=poem1&id=36647'
#headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
#response = requests.get(url=url,headers=headers)
#html_content= response.text
#print(response.text)

# 指定保存文件的路径
output_file_path = 'all_poems.txt'

# 先清空(如果存在)或创建目标文件
with open(output_file_path, 'w', encoding='utf-8') as file:
    file.write("")  # 清空文件内容

# 循环下载每首诗
for poem_id in range(36647, 36848):
    url = f'https://www.sou-yun.cn/Query.aspx?type=poem1&id={poem_id}'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.139 Safari/537.36'}
    response = requests.get(url=url, headers=headers)
    #html_content = response.text
    # 获取网页内容
    #response = requests.get(url)

    if response.status_code == 200:
        html_content = response.text

        # 提取标题
        title_match = re.search(
            r"<span class='bold'><span class='wordLink'[^>]*>(.*?)<\/span><\/span>\s*<span[^>]*>(.*?)<\/span>\s*<span class='poemAuthor'[^>]*>(.*?)<\/span>",
            html_content)
        if title_match:
            title = title_match.group(1) + title_match.group(2)  # 合并标题部分
            author = re.sub(r"<.*?>", "", title_match.group(3)).strip()  # 处理作者

            # 正则表达式匹配诗句
            poem_sentences = re.findall(r"<div class='poemSentence'[^>]*>(.*?)<\/div>", html_content, re.DOTALL)

            # 清理并准备写入文件的内容
            output = f"《 " + title + "》 ("+ author + ")\n"

            for sentence in poem_sentences:
                # 移除HTML标签
                clean_sentence = re.sub(r"<.*?>", "", sentence).strip()
                if clean_sentence:  # 过滤掉空句
                    output += clean_sentence + "\n"

            # 为每首诗添加分隔线
            output += "\n" + "=" * 50 + "\n\n"  # 分隔线,用于区分不同的诗

            # 将结果追加到文本文件
            with open(output_file_path, 'a', encoding='utf-8') as file:  # 以追加模式打开文件
                file.write(output)

            print(f"信息已保存到 {output_file_path}")
        else:
            print(f"在ID {poem_id} 的页面中找不到诗的标题或作者。")
    else:
        print(f"无法获取ID {poem_id} 的页面,状态码: {response.status_code}")
        

运行结果:
在这里插入图片描述
在这里插入图片描述


http://www.kler.cn/news/341310.html

相关文章:

  • Apache DolphinScheduler社区9月进展记录
  • 鸿蒙OS 开机动画流程
  • C++:visual studio运行时找不到.dll文件
  • 概率论详细介绍
  • 【北京迅为】《STM32MP157开发板嵌入式开发指南》-第十九章 Linux 工具之make 工具和 makefile 文件
  • easyexcel多sheet导出(唯一能用)
  • PL/SQL
  • vue3实现登录获取token并自动刷新token进行JWT认证
  • 分治算法(7)_归并排序_计算右侧小于当前元素的个数
  • SpringBoot技术在服装生产管理中的实践
  • 【JDK17 | 1】Java 17 深入剖析:新特性与变革
  • 22-微服务项目部署
  • 芋道前端utils文件夹
  • package.json配置
  • Java后端面试题(day16)
  • 【万字长文】Word2Vec计算详解(二)Skip-gram模型
  • 第三方软件测评机构简析:软件安全测试报告的内容和作用
  • 第一弹:计算机网络概述与UDP通信
  • 强化学习笔记之【DDPG算法】
  • 初始化数据的正确方式?