您的位置:首页 > 编程语言 > Python开发

python爬取糗事百科数据并保存到sqlite中,命令行读出

2017-11-10 23:26 405 查看
import requests
import sqlite3
from bs4 import BeautifulSoup
class QSBK:
def __init__(self):
self.page=0
self.items=[]
def getItems(self):
print('正在读取第'+str(self.page)+'页........')
conn=sqlite3.connect('joke.db')
cursor=conn.cursor()
cursor.execute('create table if not exists jokes (id integer primary key autoincrement,item varchar,zan varchar)')
url='http://www.qiushibaike.com/text/page/'+str(self.page)
r=requests.get(url).content
soup=BeautifulSoup(r,'html.parser').find_all('div',class_='content')
soupz=BeautifulSoup(r,'html.parser').find_all('span',class_='stats-vote')
i=0
while i<len(soup):
if soup[i].span.string is not None:
try:
self.items.append(soup[i].span.string +'......'+soupz[i].i.string+'赞')
sql='insert into jokes (item,zan) values (\''+soup[i].span.string+'\',\''+soupz[i].i.string+'\')'
cursor.execute(sql)
except:
print('程序崩溃正在恢复中...')
i+=1
conn.commit()
cursor.close()
conn.close()
def loadPage(self):
if len(self.items)<2:
self.page+=1
self.getItems()
def getOne(self):
i=input()
if i=='q':
conn=sqlite3.connect('joke.db')
cursor=conn.cursor()
cursor.execute('drop table if exists jokes')
cursor.close()
conn.close()
return
else:
self.loadPage()
print(self.items[0])
del self.items[0]
self.getOne()
def start(self):
print('给你讲笑话,按回车查看新段子,q退出!')
self.getOne()
spider=QSBK()
spider.start()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: