【Python爬虫】爬取笔趣阁小说(部分)

目录

 下载单章内容

 获取每章URL,下载多章内容

下载一本小说

最后


爬取小说的网站选取的是笔趣阁,笔趣阁没有反爬,比较好爬取一点。至于其他的小说网站,或者彩票,股票,也可以参照这个思路去爬取,这几天的股市真是见证历史了。源码见我的gitee或者CSDN下载

 下载单章内容

#下载单章内容

def download_one_chapter():
    """获取网页源代码"""
    target_url = 'http://www.shuquge.com/txt/8659/25441893.html'
    response = requests.get(target_url)
    response.encoding=response.apparent_encoding
    html=response.text

    """从网页源代码中拿到小说正文信息"""
    sel = parsel.Selector(html)
    title=sel.css('.content h1::text').extract_first()
    contents = sel.css('#content::text').extract()

    """数据清除 转化并清楚空白字符串"""
    contents1=[content.strip() for content in contents]
    #print(contents1)
    text = '\n'.join(contents1)
    #print(text)

    """保存小说内容"""
    file=open(title+'.txt',mode='w',encoding='utf-8')
    #只能写入字符串
    file.write(title)
    file.write(text)
    #关闭文件
    file.close()

说明:

target_url为请求的网址,response 服务返回内容及对象;

response得到的网页源码中存在乱码问题,response.apparent_encoding自动解决编码问题;

从网页中解析数据可以使用正则表达式(多用于匹配字符串),json(字典),xpath(路径提取)以及css选择器,这里用css提取信息——标题和正文;可以在网页代码中右键复制css选择器路径;

‘extract=getall’,可以通过ctrl+左键查看函数;

css选择器得到的content是一个列表,而write()是写入字符串的,所以使用join()把列表变成字符串,并通过列表推导式
    对列表操作去除两端空白字符得到content1。

 获取每章URL,下载多章内容

"""获取书籍每章链接,目录页"""
def get_chapters_links(target_url):
    """目录页获取每章的url"""
    #target_url = 'http://www.shuquge.com/txt/8659/index.html'
    response = requests.get(target_url)
    response.encoding=response.apparent_encoding
    html=response.text

    """css选择器提取"""
    sel=parsel.Selector(html)
    links=sel.css('dd a::attr(href)').extract()
    for link in links:
        print('http://www.shuquge.com/txt/8659/'+link)
    return links

 

css提取‘dd’标签下‘a’标签中的‘href’属性下的每章的id,通过拼接找到每章的url。

下载一本小说

def get_one_book(book_url):
    links = get_chapters_links(book_url)
    for link in links:
        #print('http://www.shuquge.com/txt/8659/'+link)
        download_one_chapter('http://www.shuquge.com/txt/8659/'+link)

整合上面两个函数,获取每章url,再进行每一章的下载。


最后

后续可以获取整个站点的小说目录,下载整个网站的小说。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZoomToday

给作者倒一杯卡布奇诺

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

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

打赏作者

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

抵扣说明:

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

余额充值