爬取古诗词


闲来无事,就写了个代码爬取古诗词名句来看看。我们用到的是这个网站 链接: https://www.xungushici.com/。刚刚开始学习Python爬虫,有些不对的地方还请大佬指出。

获取内容

首先我们看一下这个内容的基本架构。分别有注释、故事赏析等等。在这里插入图片描述
分别获取它的位置。分别获取它的位置。
对其进行提取

def get_body(url):
    respond = requests.get(url)
    html = parsel.Selector(respond.text)
    title = html.css('body > div > div > div > div > div > h3::text').get()
    origin = html.css('body > div > div > div> div > div> p > a::text').get()
    try:   #因为部分诗词没有注释和故事,所以在这里进行报错
        note = html.css('body > div > div > div> div:nth-child(2) > div > p:nth-child(4)::text').get()
        stories = html.css('body > div > div > div > div:nth-child(3) > div > p::text').getall()
        story = "\n".join(stories)   #对列表进行拼接
    except:
        pass
    appreciates = html.css('body > div > div > div > div:nth-child(3) > div > p::text').getall()
    appreciate = '\n'.join(appreciates)

进行文件保存

    with open('title + '.txt', mode='a', encoding='utf-8') as f:
        f.write(title)
        f.write('\n')
        f.write('出自于:'+'\n'+origin+'\n')
        try:  #因为部分诗词没有注释和故事,所以在这里进行报错
            f.write('注释:'+'\n'+note+'\n')
            f.write('故事:'+'\n'+story+'\n')
        except:
            pass
        f.write('赏析:'+'\n'+appreciate+'\n')
    f.close()

实现所有网页提取和翻页

得到每一个诗词的位置在这里插入图片描述
进行编写

def get_url(url):
    respond = requests.get(url)
    html = parsel.Selector(respond.text)
    url_links = html.css('body > div > div > div > div > div > ul > li > a::attr(href)').getall()
    for url_link in url_links:
        url_link = 'https://www.xungushici.com'+url_link
        print(url_link)
        get_body(url_link)

再分析每一页
在这里插入图片描述
不难得出规律
进行编写

def get_page():
    url = 'https://www.xungushici.com/mingjus/p'
    for i in range(1,3):
        html = 'https://www.xungushici.com/mingjus/p%d'%i
        get_url(html)

完整代码

# -*- coding = utf-8 -*-
# @Time : 2021/7/3 21:02
# @File : 尝试爬取寻古诗词网.py
# @Software : PyCharm

import requests
import parsel

def get_page():
   url = 'https://www.xungushici.com/mingjus/p'
   for i in range(1,3):
       html = 'https://www.xungushici.com/mingjus/p%d'%i
       get_url(html)

def get_url(url):
   respond = requests.get(url)
   html = parsel.Selector(respond.text)
   url_links = html.css('body > div > div > div > div > div > ul > li > a::attr(href)').getall()
   for url_link in url_links:
       url_link = 'https://www.xungushici.com'+url_link
       print(url_link)
       get_body(url_link)

def get_body(url):
   respond = requests.get(url)
   html = parsel.Selector(respond.text)
   title = html.css('body > div > div > div > div > div > h3::text').get()
   origin = html.css('body > div > div > div> div > div> p > a::text').get()
   try:
       note = html.css('body > div > div > div> div:nth-child(2) > div > p:nth-child(4)::text').get()
       stories = html.css('body > div > div > div > div:nth-child(3) > div > p::text').getall()
       story = "\n".join(stories)   #对列表进行拼接
   except:
       pass
   appreciates = html.css('body > div > div > div > div:nth-child(3) > div > p::text').getall()
   appreciate = '\n'.join(appreciates)
   with open(title + '.txt', mode='a', encoding='utf-8') as f:
       f.write(title)
       f.write('\n')
       f.write('出自于:'+'\n'+origin+'\n')
       try:
           f.write('注释:'+'\n'+note+'\n')
           f.write('故事:'+'\n'+story+'\n')
       except:
           pass
       f.write('赏析:'+'\n'+appreciate+'\n')
   f.close()

get_page()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值