下面我创建了一个脚本,它解析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()