Python爬虫案例Demo——小说的爬取

这个寒假闲来没事开始爬虫,学了几天了,期间自己爬了一些网站,现在大致整理一下:

import requests
import re
response = requests.get("http://www.shuquge.com/txt/96636/index.html")
# 万能设置编码
response.encoding = response.apparent_encoding
html = response.text

#正则表达式获取所有的章节目录和url网址
result = re.findall('<dd><a href="(.*?)">(.*?)</a></dd>',html)
print(result)

for url,name in result:
    new_url = "http://www.shuquge.com/txt/96636/" + url
    response1 = requests.get(new_url)
    response1.encoding = response1.apparent_encoding
    html = response1.text
    result1 = re.findall('<div id="content" class="showtxt">(.*?)</div>',html,re.S)
    # re.S是自动换行
    with open(str(name) + ".txt", mode="w", encoding="utf-8") as f:
        f.write(str(result1[0]).replace("&nbsp;&nbsp;&nbsp;&nbsp;","").replace("<br/>",""))

"""
print("http://www.shuquge.com/txt/96636/" + url)
print(name)
response1 = requests.get("http://www.shuquge.com/txt/96636/28767693.html")
response1.encoding = response1.apparent_encoding
html = response1.text
with open("1.html",mode="w",encoding="utf-8") as f:
    f.write(html)
"""

爬取的是小说《抢救大明朝》,Pycharm截止到我现在运行这些代码还是可以的,大家可以看截图:
在这里插入图片描述
爬取最后的小说界面是:
在这里插入图片描述
这是爬虫第一天敲的案例!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值