用python爬取笔趣阁小说---python爬虫

需要的导入的包:

import requests
import re

爬取笔趣阁小说:

https://www.biquge.com.cn/

最近在看《超神机械师》就以这个为例分析爬取代码

先到小说的详情页面:

https://www.biquge.com.cn/book/29105/

检索之后可以发现每章小说的网址:
在这里插入图片描述
用re.findall 获取这些网址保存起来待用:
在这里插入图片描述
因为这些网址只是后半部分,我们可以加上后半部分,访问章节网址,获取小说:
在这里插入图片描述
最后保存到文档中就行了:
在这里插入图片描述

import requests
import re
#请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7',
    'Referer': 'https://www.biquge.com.cn/'
}
#网址
url = 'https://www.biquge.com.cn/book/29105/'
urls = 'https://www.biquge.com.cn'
#获取text
req = requests.get(url,headers = headers)
html = req.text
#获取每章节的网址
zhangjie = re.findall('<dd><a href="(.*?)"  >.*?</a></dd>',html,re.S)
mulu = re.findall('<dd><a href=".*?"  >(.*?)</a></dd>',html,re.S)

i = 0
for url in zhangjie:
    requ = requests.get(urls+url,headers = headers)
    htmls = requ.text
    #获取小说文本
    fiction = re.findall('<div id="content">&nbsp;&nbsp;&nbsp;&nbsp;(.*?)</div>',htmls,re.S)
    fiction = str(fiction)
    #消去多余字符
    fiction = re.sub("[|]|<br><br>&nbsp;&nbsp;&nbsp;&nbsp;|<br>",'',fiction)
    print(mulu[i])
    print('======保存中======')
    #保存到文本中
    print(fiction[2:-2])
    with open('超神机械师1.txt','a+') as f:
        fictions = str(mulu[i]) + '\n' +fiction[2:-2]+'\n'
        f.writelines(str(fictions))
        i +=1
        print('=====已保存=====')
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值