背景

很多人说学习爬虫是提升自己的一个非常好的方法,所以有了第一次使用爬虫,水平有限,依葫芦画瓢,主要作为学习的记录。

思路

  1. 使用python的requests模块获取页面信息
  2. 通过re模块(正则表达式)取出需要的内容(小说标题,正文)
  3. 通过MysqlDB模块入库
  4. 使用webpy模块生成访问页面

最终的效果图

下面是效果图,简单实现了点击上一页、下一页翻页的功能:

目录结构

目录结构如下:

D:\PROJECT\SPIDER
│  fiction_spider.py
│  webapp.py

└─template
        index.html

爬取信息及入库示例代码

#coding:utf-8#fiction_spider.py
import requests
import re
import MySQLdb

def get_title():
    html = requests.get('http://www.jinyongwang.com/tian/').content
    rem = r'<li><a href="(.*?)">(.*?)</a>'
    return  re.findall(rem,html)

def get_content(url):
    html = requests.get('http://www.jinyongwang.com/'+url).content
    #print html
    matchs_p = r'<p>(.*?)</p><script.*?'
    data = re.findall(matchs_p, html)
    return data[0]

if __name__ == '__main__':
    a = MySQLdb.connect(host='10.1.*.*', port=3306, user='user', passwd='passwd', db='testdb', charset='utf8')
    for i in get_title():
        cur = a.cursor()
        print i[1]
        print i[0]
        sqli = 'INSERT INTO `fiction` (`title`, `content`) VALUES ("%s","%s" )'%(i[1],get_content(i[0]))
        cur.execute(sqli)
        cur.close()
        a.commit()
    a.close()

生成访问页面示例代码

#coding:utf-8#webapp.py
import web
import re

urls = ('/(.*)','Index')

db = web.database(dbn = 'mysql',host='10.1.*.*', port=3306, user='user', passwd='passwd', db='testdb', charset='utf8')

render = web.template.render('template')

class Index:
    def GET(self,html):
        id = re.findall('(.*?).html',html)[0]
        print id
        data = db.query("select * from fiction where id=%s"%id)
        return render.index(data[0],id)

if __name__ == '__main__':

    web.application(urls,globals()).run()

页面访问的index.html内容如下:

$def with(data,s)
<meta charset="utf-8"/>
<title>$:data.title</title>
<h1>$:data.title</h1>
<div style="margin:0px auto;text-align:center;">
<a href="$:(int(s)-1).html">上一页</a>
<a href="$:(int(s)+1).html">下一页</a>
</div>
$:data.content
<br>
<div style="margin:0px auto;text-align:center;">
<a href="$:(int(s)-1).html">上一页</a>
<a href="$:(int(s)+1).html">下一页</a>
</div>

保存到txt:

if __name__ == '__main__':
    a  = open(u'射雕**传.txt','w')
    m = 0
    for i in get_title():
        #print i[1], get_content(i[0])
        time.sleep(2)
        data = i[1] + '\n' + '\n' + get_content(i[0]).replace('</p><p>','\n\n') + '\n\n' #在标题和内容之间插入换行符,将html中的<p>参数变成换行符
        a.writelines(data)
        m += 1
        print u'正在爬取第%s段内容' % m
        # if m >2:
        #     print u'正在爬取第%s段内容' % m
        #     break
    a.close()

【Python】简单实现爬取小说《天龙八部》,并在页面本地访问的更多相关文章

  1. 初次尝试python爬虫,爬取小说网站的小说。

    本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...

  2. Python简单程序爬取天气信息,定时发邮件给朋友【高薪必学】

    前段时间看到了这个博客.https://blog.csdn.net/weixin_45081575/article/details/102886718.他用了request模块,这不巧了么,正好我刚用 ...

  3. python简单爬虫爬取百度百科python词条网页

    目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...

  4. 04 Python网络爬虫 <<爬取get/post请求的页面数据>>之requests模块

    一. urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib ...

  5. python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)

    转载出处:药少敏   ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...

  6. Scrapy爬取小说简单逻辑

    Scrapy爬取小说简单逻辑 一 准备工作 1)安装Python 2)安装PIP 3)安装scrapy 4)安装pywin32 5)安装VCForPython27.exe ........... 具体 ...

  7. Python实战项目网络爬虫 之 爬取小说吧小说正文

    本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...

  8. Golang 简单爬虫实现,爬取小说

    为什么要使用Go写爬虫呢? 对于我而言,这仅仅是练习Golang的一种方式. 所以,我没有使用爬虫框架,虽然其很高效. 为什么我要写这篇文章? 将我在写爬虫时找到资料做一个总结,希望对于想使用Gola ...

  9. python之爬取小说

    继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...

随机推荐

  1. notepad快捷键总结

    notepad快捷键总结 常用快捷键: 快捷键 功能1.Ctrl-D 复制当前行2.Ctrl-L 删除当前行3.Ctrl-T 把当前行和前面一行调换位置4.F11 切换全屏模式5.Ctrl-Shft- ...

  2. mysql ERROR 1264 (22003): Out of range value for column 'x' at row 1 错误

    mysql> insert into t1 values (-129), (-128), (127),(128);ERROR 1264 (22003): Out of range value f ...

  3. java 多线程 day07 多线程共享数据

    /** * Created by chengtao on 17/12/3. * 多个线程 如何共享数据? * 常见实例:多个窗口同时售卖火车票 */public class Thread0701_Mu ...

  4. sipp模拟freepbx分机测试(SIP协议调试)

    1.sipp的安装 1) 在centos 7.2下安装 yum install make gcc gcc-c++ ncurses ncurses.x86_64 ncurses-devel ncurse ...

  5. HDU2425:Hiking Trip(简单bfs,优先队列实现)

    题目: 传送门 题意很简单就不解释了,水题一道. #include <iostream> #include <string.h> #include <stdio.h> ...

  6. PAT 1048 Find Coins[比较]

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. Java中的反射[转载]

    转自:https://blog.csdn.net/sinat_38259539/article/details/71799078#commentBox 1.什么是反射? 反射是通过一个类可以知道其中所 ...

  8. MFC中对基于ODBC对数据ACCESS数据库的增删改查。

    在MFC中可以使用很多方法对数据库进行操作. 什么ODBC  什么ADO之类的,这里要介绍使用的ODBC这种方法,通过本文的阅读可以达初步掌握在MFC里面通过ODBC访问ACCESS数据库. 涉及到的 ...

  9. iOS 定位方式 iOSNsPredicateString 详解

    原文地址https://segmentfault.com/a/1190000010205649 前言 由于使用id.className.AccessibilityId定位方式较为简单,多数情况下,在同 ...

  10. python全栈开发从入门到放弃之socket并发编程多进程

    1.1 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程 ...