python网络爬虫(第五章:实战5:bs4之数据解析)

实战1. 爬取三国演义小说所有章节标题和章节内容

# https://www.shicimingju.com/book/sanguoyanyi.html
import requests
from bs4 import BeautifulSoup

if __name__ == "__main__":
    #1.指定url
    url ='https://www.shicimingju.com/book/sanguoyanyi.html'
    header = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
    }
    #2.发送get请求
    response = requests.get(url=url,headers=header).text.encode('ISO-8859-1')   #requests.get() 获取的是响应对象    #.text获取的是响应数据

    # 3.实例化BeautifulSoup对象,需要将页面源码数据加载到该对象中
    soup = BeautifulSoup(response,'lxml')
    # 4.解析章节标题
    li_list = soup.select('.book-mulu > ul > li > a')

    fp = open('./sanguoyan.txt', 'w', encoding='utf-8')  #创建txt格式的文件夹

    for li in li_list:
        #解析章节标题
        title = li.string     #li标签下的a标签中的直系内容,则使用string
        #准备解析章节内容:
        detail_url = 'https://www.shicimingju.com' + li['href']
        #对详情页发送请求,解析出章节内容
        detail_page_text = requests.get(url=detail_url,headers=header).text.encode('ISO-8859-1')
        #解析出详情页中相关的章节内容
        detail_soup = BeautifulSoup(detail_page_text,'lxml')
        #解析到了章节的内容
        detail_tag = detail_soup.find('div',class_='chapter_content').text
        
        fp.write(title+":"+ detail_tag +'\n')
        print(title,'成功抓取到!!!')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值