import requests
from bs4 import BeautifulSoup
import sqlite3
def opendb():
con = sqlite3.connect("D:/realtimehot.db")
cur = con.execute("""create table if not exists realtimehot(snum text primary key, swords text, slink text)""")
return cur, con
def adddb(num, words, link):
hel = opendb()
hel[1].execute("insert into realtimehot(snum, swords, slink)values(?, ?, ?)", (num, words, link))
hel[1].commit()
hel[1].close()
def showalldb():
hel = opendb()
cur = hel[1].cursor()
cur.execute("select * from realtimehot")
for line in cur:
print(line)
cur.close()
num = i
words = str(data[i].text)
link = 'https://s.weibo.com' + str(data[i]['href'])
adddb(num, words, link)
showalldb()
Python语言-编写一个网络爬虫程序,将文字和图像等信息抓取到sqlite中保存(实时爬取微博热搜数据)
最新推荐文章于 2024-03-20 13:11:09 发布