古诗文学网爬取

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

  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----我说为了好看你信吗?🙄

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬虫是一种按照一定规则,自动抓取万维信息的程序或者脚本。它可以通过定义好的规则,自动抓取络上的信息,并对抓取下来的数据进行筛选和提取,从而获得我们需要的信息。爬虫并不是Python的专利,使用其他编程语言也可以实现爬虫功能,但Python提供了许多方便的库,使得开发爬虫变得更加简单。\[1\] 在爬取古诗的例子中,使用了Python的requests库和BeautifulSoup库来进行页请求和解析。首先,通过发送HTTP请求获取页的HTML内容,然后使用BeautifulSoup库对HTML进行解析,提取出需要的信息,如标题、朝代、作者和内容。最后,将提取的信息存储到一个列表中,并将列表写入一个JSON文件中。\[2\] 另外,还可以使用XPath解析HTML内容。XPath是一种用于在XML和HTML文档中进行导航和提取信息的语言。在这个例子中,使用了Python的requests库和lxml库来进行页请求和解析。通过XPath表达式,可以直接定位到需要的元素,并提取出相应的信息,然后将提取的信息存储到一个列表中,并将列表写入一个JSON文件中。\[3\] 总结来说,Python爬虫可以通过发送HTTP请求获取页内容,然后使用相应的库对页进行解析,提取出需要的信息,并进行存储和处理。这样就可以实现对古诗或其他站的信息进行爬取。 #### 引用[.reference_title] - *1* [Python爬虫(一)——爬取古诗,初识什么是爬虫](https://blog.csdn.net/u014663232/article/details/103459450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python爬虫入门_3种方法爬取古诗站](https://blog.csdn.net/purvispanwu/article/details/106849214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值