学习爬虫的第一个案例是小说爬虫。

小说爬虫首先是解析小说页面源代码,在页面源代码中可以看到小说每章节的内容链接

爬虫的代码:

import requests
import re url = 'http://www.92kshu.cc/69509/'
response = requests.get(url)
response.encoding = 'gbk'
html = response.text
title = re.findall(r'<meta property="og:novel:book_name" content="(.*?)"/>', html)[0]
fb = open('%s.txt' % title, 'w', encoding='utf-8')
# 获取每章的内容
# print(html)
dl = re.findall(r'<dl><dt><i class="icon"></i>正文</dt>(.*?)</dl>', html)[0]
print(dl)
chapter_info_list = re.findall(r'<dd><a href="(.*?)">(.*?)</a></dd>', dl)
print(chapter_info_list)
for chapter_info in chapter_info_list:
chapter_url, chapter_title = chapter_info
chapter_url = "http://www.92kshu.cc%s" % chapter_url
# print(chapter_url)
chapter_response = requests.get(chapter_url)
chapter_response.encoding = 'gbk'
chapter_html = chapter_response.text
chapter_content = re.findall(r'<div class="chapter">(.*?)><br>', chapter_html)[0]
# print(chapter_content)
chapter_content = chapter_content.replace('<p>', '')
chapter_content = chapter_content.replace('</p>', '')
fb.write(chapter_title)
fb.write(chapter_content)
fb.write('\n')
print(chapter_url)

爬虫结果:

最新文章

  1. android 使用LinearGradient进行字体渐变的效果
  2. 从SqlServer现有数据生成Insert脚本
  3. Populate A List Item With Record Group In Oracle Forms Using Populate_List And Create_Group_From_Query Command
  4. 自定义Angular指令与jQuery实现的Bootstrap风格数据双向绑定的单选&amp;多选下拉框
  5. C# 各种集合
  6. SQL从入门到基础 - 04 SQLServer基础2(数据删除、数据检索、数据汇总、数据排序、通配符过滤、空值处理、多值匹配)
  7. git安装及使用简介
  8. RF+Appium框架自动化测试系列一之(Mac下Appium环境搭建)万事开头难
  9. backupMysql.sh
  10. 04-PHP-redis
  11. wince可用的7-zip
  12. 微信小程序开发-tabbar组件
  13. 持续集成一:git上传代码
  14. psql工具使用(二)
  15. Mono.Cecil 修改目标.NET的IL代码保存时报异常的处理。
  16. vim 中替换命令
  17. 本地搭建sass运行环境
  18. sea.js中的checkbox批量操作
  19. 如何轻松学习C语言编程!
  20. [技巧篇]21.Android Studio的快捷键设置[图片版]

热门文章

  1. mcast_get_if函数
  2. css3 :default应用场景
  3. 「JSOI2014」歌剧表演
  4. 2019年ipa发布苹果应用商店审核指南
  5. python 基础之深浅拷贝
  6. pycharm自定义代码段
  7. java list 清空列表所有元素
  8. LeetCode 445. Add Two Numbers II(链表求和)
  9. uniGUI 应用程序体系结构(11)
  10. hbase meta中分区信息错误的记录