html目录sqlite3,解析HTML并存储在SQLITE3中,都是旧的d

下面我创建了一个脚本,它解析HTML,然后将列表分解为变量,以分配给正确的列。我如何才能做到这一点,使程序拉入所有的数据,从过去的网站,所以我不必分配一个新的'a,b,y'变量每次?另外,如果有人能帮我。分割日期(如果你看html文本解析你会看到,有一个日期之前的描述现在我只是测试作为一个占位符为y)。你知道吗import bs4

import requests

from pprint import pprint

import sqlite3

def get_elems_from_document(document):

pass

res = requests.get('http://www.sharkresearchcommittee.com/pacific_coast_shark_news.htm')

res.raise_for_status()

soup = bs4.BeautifulSoup(res.text, 'html.parser')

news = [p.text.strip() for p in soup.select('h1 ~ p') if p.find('font')]

a, b= (str(news[0]).split(" \xa0 — \xa0 "))

y = 'test'

c = sqlite3.connect('shark.db')

try: ## if a table already existis, and you execute a create table an operational error will be thrown because it's trying to create another table that alrady exists(adding a new column after running could pose issue, essentially you have to delete db file and recreate it with new colum)

c.execute('''CREATE TABLE mytable (

Location STRING,

Date STRING,

Description STRING )''');

except sqlite3.OperationalError: #i.e. table exists already

pass

c.execute('''INSERT INTO mytable(Location,Date,Description) VALUES(?,?,?)''',

(a, y, b))

c.commit()

c.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值