python爬虫练习-爬取小说

  •  详细过程及步骤(待补充全站下载)
# _*_ coding :utf-8 _*_
#@Time :2021/10/23 19:33
#@File :爬虫练习_爬取小说剑来
#@Project :
import requests
import parsel
import time

url = 'https://www.shuquge.com/txt/8659/2324752.html'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
}
#1.发送请求
#2.获取源数据
#3.进行解析并保存数据


#1.发送请求并获取源数据

def down_book(url,book_name,p):
    response=requests.get(url=url,headers=headers)
    #设置响应的编码格式
    #response.encoding=response.apparent_encoding
    response.encoding='utf-8'
    #获取源数据
    #print(response.text)

    #2.进行解析
    #2.1xpath解析
    #(1)导入需要的库函数
    # from lxml import etree
    # (2)将获取的字符串数据变成xpath可解析的对象
    # bt=etree.HTML(response.text)
    #(3)寻找路径(获取内容时候使用text()获取文本内容,xpth解析后得到的是字符串形式)
    # title_name=bt.xpath('//div[@class="content"]/h1')
    # content_text=bt.xpath('//div[@id="content"]/text()')
    # print(title_name[0].text)
    # for i in range(len(content_text)):
    #     print(content_text[i])
    #print(bt)

    #2.2css解析
    #css解析时候,使用get()方法获取文字内容;属性提取器attr
    #(1)导入需要的库
    import parsel
    #(2)将字符数据转换成可解析的对象
    bt=parsel.Selector(response.text)
    #(3)寻找路径,获取数据
    title_name=bt.css('.reader h1::text').get()
    content_text=bt.css('.showtxt ::text').getall()
    print(title_name)
    new_content=[]
    for i in content_text:
        new_content.append(i.strip())
    new_content1='\n'.join(new_content)
    #print('\n'.join(new_content))

    #3.保存数据
    file=open(book_name+str(p)+'.txt',mode='a',encoding='utf-8')
    #file.write(title_name)
    file.write('\n')
    file.write(new_content1)
    file.close()

#https://www.shuquge.com/txt/8659/2324752.html
#https://www.shuquge.com/txt/8659/2324753.html

url='https://www.shuquge.com/txt/8659/index.html'
new = requests.get(url=url, headers=headers)
new.encoding = 'utf-8'
new1=parsel.Selector(new.text)
# 设置响应的编码格式
# response.encoding=response.apparent_encoding
book_name=input('bookname:')
p=0
xh=new1.css('.listmain dl dd a::attr(href)').getall()
for i in xh:
    print(i)
    url_new=url[:-10]+i
    p=p+1
    down_book(url_new,book_name,p)
    time.sleep(1)




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D_lullaby

你的支持是对我最大的鼓励!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值