【Python】简单实现爬取小说《天龙八部》,并在页面本地访问

news/2024/7/19 8:40:34 标签: python, 爬虫, 数据库

背景

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

思路

  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()

 

转载于:https://www.cnblogs.com/Detector/p/7604151.html


http://www.niftyadmin.cn/n/785355.html

相关文章

企业数据爬虫项目(二)

企业数据爬虫项目&#xff08;艳辉VIP项目&#xff09;第一天&#xff1a;下载解析网站页面第二天&#xff1a;多线程定时启动爬虫第一天&#xff1a;下载解析网站页面 第二天&#xff1a;多线程定时启动爬虫 一个爬虫项目&#xff0c;会涉及到数据存储&#xff0c;Queue队列…

关于ckeditor 之 上传功能

度了很多文章&#xff0c;看了很多关于ckeditor配置上传功能的文章&#xff0c;没一个写得清楚的&#xff0c; 就是简单的根目录下.config.js 增加 config.filebrowserUploadUrl"/admin/film/ckeditorUpload"; //文件浏览上传地址然后就是 \plugins\image\dialogs 的…

一起艳学dubbo

一起艳学dubbo 想学习分布式&#xff0c;就从dubbo学起&#xff0c;不要问为什么&#xff0c;因为阿里曾经开源并使用过dubbo。学dubbo&#xff0c;也要从zookeeper学起&#xff0c;动物园的世界你要了解&#xff0c;食物链有金字塔&#xff0c;就有攻击者和被攻击者&#xff0…

Python 函数之装饰器

1、函数 #### 第一波 #### def foo():print foofoo #表示是函数 foo() #表示执行foo函数#### 第二波 #### def foo():print foofoo lambda x: x 1foo() # 执行下面的lambda表达式&#xff0c;而不再是原来的foo函数&#xff0c;因为函数 foo 被重新定义了2、需求 初创…

SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇

目录 1-1 Spring入门篇专题一、IOC接口及面向接口编程什么是IOCSpring的Bean配置Bean的初始化Spring的常用注入方式专题二、BeanBean配置项Bean的作用域Bean的生命周期AwareBean的自动装配(Autowiring)ResourcesBean管理的注解实现及例子专题三、AOP什么是AOP切面AOP实现方式AO…

java将多张图片合成视频

java将多张图片合成视频 需求 近几天&#xff0c;无聊就看看抖音&#xff0c;视频信息传播信息&#xff0c;相亲去抖音&#xff0c;网红去抖音&#xff0c;秀恩爱去抖音。。。走在大街上&#xff0c;几个小妹妹拿着手机自拍干是玩抖音还是直播呢&#xff1f;每个人都想当导演…

MySQL InnoDB Update和Crash Recovery流程

MySQL InnoDB Update和Crash Recovery流程 概要信息首先介绍了Redo&#xff0c;Undo&#xff0c;Log Sequence Number (LSN)&#xff0c;Checkpoint&#xff0c;Rollback Pointer (ROLL_PTR)&#xff0c;Transaction ID (TRX_ID)&#xff0c;Transaction Serialization Number(…

移动端网页录音上传,服务端智能语音识别

移动端网页录音上传&#xff0c;服务端智能语音识别 最近&#xff0c;看了创业时代的魔镜&#xff0c;想法突如起来&#xff0c;能不能手机发送一条语音&#xff0c;语音上传到后台&#xff0c;自动识别语音的信息&#xff0c;转化为文字&#xff0c;将文字分析&#xff0c;然…