古诗文学网爬取

爬取故事文学网小,中,高所有故事、文言文

  1. 首先看一下网站的首页简单分析一下所需的内容在在诗文页面当中
    在这里插入图片描述
  2. 点击两个不同类型网站1网站2
    在这里插入图片描述
    获取页面中所有的url,发现url全部在herf标签中
    在这里插入图片描述
    进入精确的url中,获取这部分的内容
    在这里插入图片描述
    在这里插入图片描述

查看源代码,一开始以为很顺利,结果却报错了,对比其他页面才发现id="contson后面的值是跟随url变的


本以为接下来会一切顺利,接下来还报错了,仔细一看才发现原来古诗和文言文的url格式并不一样
在这里插入图片描述

一个是.cn另一个是.org,现在才发现,不注意细节的我毁了多少女子的温柔
再次忠告:注意细节!!!!!
好了,话不多说代码赠上

import requests
import re
import time

headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTMLl like Gecko) Chrome/81.0.4044.92 Safari/537.36'
    }
def kind(url):#获取小学,初中,高中的诗词的url,name,作者,朝代
    html=requests.get(url=url,headers=headers)
    html.encoding='utf-8'
    html=html.text
    shi_info=re.findall('<div class="main3">(.*?)<div id="btmwx"',html,re.S)
    shi_info=str(shi_info)
    shi_list=re.findall('<span><a href="(.*?)" target="_blank">(.*?)</a>(.*?)</span>',str(shi_info))
    for url2,name,zz in shi_list:
        main(url2,name,zz,url)
def main(url,name,zz,s):#获取内容
    try:
        html=requests.get(url=url,headers=headers)
        html.encoding='utf-8'
        html=html.text
        key=re.findall('https://so.gushiwen.cn/shiwenv_(.*?).aspx',url)[0]
        shi=re.findall('<div class="contson" id="contson{}">(.*?)</div>'.format(key),html,re.S)[0].replace('\n','').replace('<br />','').replace('<p>','').replace('</p>','').replace(' ','')
        All=name+'@'+name+','+zz+','+shi+'\n'
        #w(All)
        print('='*80)
        print(name+'获取并输出完毕'+'内容节选:------'+shi[10:16]+s[30:39])
        print('='*80)
    except:
        html=requests.get(url=url,headers=headers)
        html.encoding='utf-8'
        html=html.text
        key=re.findall('https://so.gushiwen.org/shiwenv_(.*?).aspx',url,re.S)[0]
        shi=re.findall('<div class="contson" id="contson{}">(.*?)</div>'.format(key),html,re.S)[0].replace('\n','').replace('<br />','').replace('<p>','').replace('</p>','').replace(' ','')
        All=name+'@'+name+','+zz+','+shi+'\n'
        w(All)
        print('='*80)
        print(name+'获取并输出完毕'+'内容节选:------'+shi[10:16]+s[30:39])
        print('='*80)
def w(info):#写入txt数据库
    f=open('main.txt','a',encoding='utf-8')
    f.write(info)
    f.close()
    time.sleep(2)

urls=['https://so.gushiwen.cn/wenyan/xiaowen.aspx','https://so.gushiwen.cn/wenyan/chuwen.aspx','https://so.gushiwen.cn/wenyan/gaowen.aspx','https://so.gushiwen.cn/gushi/xiaoxue.aspx','https://so.gushiwen.cn/gushi/chuzhong.aspx','https://so.gushiwen.cn/gushi/gaozhong.aspx']#古诗url已下载
#urls_wen=['https://so.gushiwen.cn/wenyan/xiaowen.aspx','https://so.gushiwen.cn/wenyan/chuwen.aspx','https://so.gushiwen.cn/wenyan/gaowen.aspx']#文言文url
for url in urls:
    print(url)
    kind(url)
    time.sleep(30)

在这里插入图片描述
在这里插入图片描述

至于为什么要以这种格式保存,emmmmm----我说为了好看你信吗?🙄

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值