1. 程式人生 > >python爬取糗事百科資料並儲存到sqlite中,命令列讀出

python爬取糗事百科資料並儲存到sqlite中,命令列讀出

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