爬取经典名著文学三国演义

#导包
import requests
from bs4 import BeautifulSoup
url='http://www.gushicimingju.com/novel/sanguoyanyi/'
headers={"User-Agent":
             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55"
         }#伪装
response=requests.get(url=url,headers=headers).text#得到了她的一个就是说来源数据
#print(response)
#创建一个txt文档用来保存爬取到的小说内容  在循环之外
with open('./doupo.txt','w',encoding='utf-8') as fp:
    #实例化一个BeautifuiSoup对象 并且获取章节目录和章节url
    #实例化B对象
    soup=BeautifulSoup(response,'lxml')
    #将使用select进行一个层次定位 获得li便签的一个列表
    page_li=soup.select('.main-content > ul > li')
    #print(page_li)
    for li in page_li:#对li进行遍历
        title=li.a.string#获得li。a里面的第一条文本 即title标题
        page_url='http://www.gushicimingju.com/'+li.a['href']#获取li。a里面的属性href 添加部分网址 获得小说内容网站
        page_data=requests.get(url=page_url,headers=headers).text#对小说内容网站进行爬取
        soup_data=BeautifulSoup(page_data,'lxml')#对该内容进行一个B对象实例化
        b_data=soup_data.find('div',class_='shici-content check-more')
        data=b_data.text
        #print(data)
        fp.write(title+':'+data+'/n')#存入
        print(title,'下载完成!!!!')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值