背景

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

思路

  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. 02-django查询

      目录 (一)查询 1 .基本查询(等于.大于.包含字符.日期.字段比较.逻辑) 2 .关联查询(即为join查询)(一对多.多对多.一对一) 3 .聚合查询(统计查询) (二)关联对象(已知A表的 ...

  2. POJ 3171

    题目大意:        给定一个区间范围[M,E],接下来有n行输入.每行输入三个数值:T1,T2,S,表示覆盖区间[T1,T2] 的代价为S,要求你求出覆盖区间[M,E]的最小代价,假设不能覆盖, ...

  3. js 屏蔽浏览器右键菜单

    <script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...

  4. BD面试题1-两个大文件中找出公共记录[转载]

    转自:https://blog.csdn.net/tiankong_/article/details/77234726#commentBox 1.题目 给定a.b两个文件,各存放50亿个url,每个u ...

  5. Mock Server 之 moco-runner 使用指南一

    文章出处http://ju.outofmemory.cn/entry/96866 用以下命令可以启动moco-runner 服务 java -jar moco-runner-<version&g ...

  6. memcache服务端与客户端的安装部署

    1)安装memcached前需要先安装libevent [root@aliyun tools]# tar -zxf libevent-1.4.13-stable.tar.gz [root@aliyun ...

  7. PKU 3020 Antenna Placement(拆点+最小边覆盖)(最大匹配)

    题目大意:原题链接 一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 提示:看清楚题目,' ...

  8. 利用page_source抓取网页中的URL,进行链接测试

    selenium的page_source方法可以获取到页面源码,下面就把它应用到链接测试中. # coding:utf-8 __author__ = 'helen' import re,request ...

  9. cocos2dx 3.x 触摸事件

    单点触摸: bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent); void onTouchMoved(cocos2d:: ...

  10. 关于Socket和ServerSocket类详解

    Socket类 套接字是网络连接的一个端点.套接字使得一个应用可以从网络中读取和写入数据.放在两个不同计算机上的两个应用可以通过连接发送和接受字节流.为了从你的应用发送一条信息到另一个应用,你需要知道 ...