使用爬虫时,将数据爬下来之后进行插入操作,但是查看数据库的时候发现并没有任何数据插入。查看被插入数据的格式和内容时机都已经被抓下来了,搜索了很久没有找到答案,最终还是在stackoverflow上面找到了答案,教训就是:

要学会用英文搜索Google!!!

要学会用stackoverflow!!!

Google大法好,stackoverflow大法好!!!

代码如下:

# -*- coding: utf-8 -*-

import urllib,urllib2,re,time,thread,sqlite3,string

class Spider_Model:

def __init__(self):

self.page = 1

self.pages = []

self.enable = False

self.conn = None

self.cursor = None

def GetPage(self,page):

myUrl = "http://m.byr.cn/board/ParttimeJob"+"?p="+page

user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'

headers = { 'User-Agent' : user_agent }

req = urllib2.Request(myUrl, headers = headers)

myResponse = urllib2.urlopen(req)

myPage = myResponse.read()

unicodePage = myPage.decode("utf-8")

myItems = re.findall('

(.*?)',unicodePage,re.S)

items = []

for item in myItems:

if not item[0].find('class')>=0:

items.append([item[0],item[1]])

return items

def LoadPage(self):

while self.enable:

if len(self.pages)<2:

try:

myPage = self.GetPage(str(self.page))

self.page += 1

self.pages.append(myPage)

except BaseException,e:

print e

else:

time.sleep(1)

def ShowPage(self,nowPage,page):

self.conn = sqlite3.connect('123.db')

self.cursor = self.conn.cursor()

self.cursor.execute('create table if not exists forum_data (id INTEGER primary key UNIQUE, title varchar(20), attr varchar(20))')

for items in nowPage:

if page<=3:

attr = items[0].split('ParttimeJob/')

article_id = string.atoi(attr[1])

article_title = items[1]

article_attr = 'http://m.byr.cn/article/ParttimeJob/'+attr[1]

self.cursor.execute("insert into forum_data (id ,title, attr) values (?, ?, ?)",(article_id, article_title, article_attr))

self.conn.commit()

print u'第%d页' %page,article_id, article_title, article_attr

else:

self.cursor.close()

self.conn.close()

self.enable = False

break

def Start(self):

self.enable = True

page = self.page

print u'正在加载请稍后'

thread.start_new_thread(self.LoadPage,())

while self.enable:

if self.pages:

nowPage = self.pages[0]

del self.pages[0]

self.ShowPage(nowPage,page)

page += 1

print u'请按下回车浏览今日的论坛内容:'

raw_input(' ')

myModel = Spider_Model()

myModel.Start()

1、在类中的函数要使用初始化的函数中的对象时,要在前面加self.conn,self.cursor。

2、在每次插入数据后,要使用self.commit进行提交,而不是最后一并在提交,只有close是在最后一次使用。

python无效数据怎么办_Python使用sqlite插入数据无效的原因相关推荐

  1. python: 在txt文档开始位置插入数据

    python: 在txt文档开始位置插入数据 python: 在txt文档开始位置插入数据 常见的在文章末尾追加数据很常见,在开头追加数据不常遇见 代码如下 代码片. import ospath = ...

  2. python csv文件创建时间_记录 python 根据csv文件自动创建表插入数据

    # -*- coding: utf-8 -*- importos importpymysql importpandas aspd # 1.连接 Mysql 数据库 try: conn = pymysq ...

  3. mysql 字段写入_MySQL为数据表的指定字段插入数据

    username not null 没有默认值/有默认值   insert不插入username字段 均不报错 2014年07月23日21:05    百科369 MySQL为数据表的指定字段插入数据 ...

  4. node sqlite 插入数据_方便且实用,Python内置的轻量级数据库实操

    之前我们在很多项目案例中都使用了MySQL数据库.有的同学问:MySQL安装和配置太麻烦了,老是搞不定,有没有更简便的方法? 当然有咯! Python内置的sqlite3模块就是一个轻量级的数据库,不 ...

  5. node sqlite 插入数据_使用 Sequelize 操作 Sqlite3 数据库

    0x01 Sequelize 是什么? Sequelize 是一个基于 Promise 的 Node.js ORM,目前支持 Postgres.MySQL.SQLite 和 Microsoft SQL ...

  6. 用python操作mysql数据库(之批量插入数据)

    #!/usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb #建立连接 conn = MySQLdb.connect(host='127. ...

  7. Python应用02--批量往Mysql数据库插入数据

    一.问题 有时候接口调试,需要往数据库导入测试数据,通常我们会手动写一些插入语句或者在controller里写一个批量插入的方法,但是总不得劲,比较繁琐.如今已经迈入人工智能的天下,还在用这样老土的方 ...

  8. python 更新数据库历史_python 实现数据库中数据添加、查询与更新的示例代码

    一.前言 最近做web网站的测试,遇到很多需要批量造数据的功能:比如某个页面展示数据条数需要达到10000条进行测试,此时手动构造数据肯定是不可能的,此时只能通过python脚本进行自动构造数据:本次 ...

  9. python 画折线图_Python让你的数据生成可视化图形

    ID:pk哥 公众号:Python知识圈 最近的技术分享被老板说了,分享内容不错,可是这些统计图差了点.作为一个做技术的,这是不能忍受的.因为 Python 除了不会生孩子,其他的都会. 直接进入今天 ...

最新文章

  1. python创建图片对应的csv格式_Python:如何从csv文件创建图形节点和边?
  2. 即学即用的 30 段 Python 非常实用的代码
  3. 嵌入式系统之温限使用
  4. 只有程序员才能看懂的30张图,绝配!
  5. c++ 管理员身份_CATIA的管理员模式和多版本环境变量设置
  6. rust房子 如何拆除_“一户多宅”将陆续清查!违规如何处理?
  7. Reactive Streams规范及常见库
  8. mailscanner参数
  9. OpenCV在Android设备上运行深度网络
  10. angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  11. Deploy Oracle 10.2.0.5 DataGuard on Red Hat Enterprise Linux 6.4
  12. java爬虫微信公众号信息_微信公众号爬虫项目(reptile)
  13. java课设——租房管理系统
  14. 设备功耗计算专题《低功耗实战篇,全志XR808连接AP的最低功耗研究》
  15. 牛逼的Android UI
  16. XILINX FPGA时钟资源
  17. cfg80211 subsystem中的wiphy
  18. 我的开源项目从0到1024的过程
  19. 蚁群算法ACO求解TSP问题
  20. c ajax 图表,Pyechart Django:前端和后端分离(Ajax),多图表组合显示,pyechartsdjango,前后,ajax,合并,展示...

热门文章

  1. 【python】数据结构与算法之二分查找
  2. 01-简述cache的基本概念和使用场景
  3. (64)句柄表,遍历所有进程的句柄表实现反调试
  4. 2020-11-28(不定参数的函数)
  5. 都21世纪20年代了,还有人问我学网络安全干嘛,你自己看吧
  6. 薪资那么高的Web前端的工作者,想要提高薪资就必须看的书单
  7. 解决matplotlib中文乱码问题
  8. 用C语言输出一个字符串的所有子串
  9. quartz配置_基于spring-boot 2.x +quartz 的CRUD任务管理系统
  10. Springboot+Docker+RabbitMQ教程来了。