我使用sqlite存储某些股票的一系列价格 我无法登录该网站,所以我在本地保存了多次网页以跟踪不同的价格。
我的代码现在适用于我之前制作的两个保存,但是当我尝试使用今天的任何保存时,我收到以下错误
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 415367:character maps to <undefined>
我的代码如下
from bs4 import BeautifulSoup
from time import gmtime, strftime
import sqlite3
from sqlclean import *
count = 0
def create_tables(stock):
sql_command = """CREATE TABLE """ + stock +"""(
Stock_number INTEGER PRIMARY KEY,
BuyPrice REAL,
SellPrice REAL,
Time VARCHAR(30));"""
cursor.execute(sql_command)
def fill():
y = 0
for i in stock:
string = sqstring(i)
stock[y] = string
y = y + 1
for i in stock:
create_tables(str(i))
def populate():
x = 0
for i in stock:
cursor.execute("""
INSERT INTO """+ i +"""
(SellPrice,BuyPrice)
VALUES
(""" + sell[x]+""","""+ buy[x] +""")
""")
x = x + 1
def get_stocks(soup):
global count
rep1 = 0
rep2 = 0
if count == 0:
count = count + 1
for price in soup.find_all('span',{"class" : "tbox-list-button-sell"}):
sell.append(price.text)
for price in soup.find_all('span',{"class" : "tbox-list-button-buy"}):
buy.append(price.text)
for price in soup.find_all('div',{"class" : "window_title list-title"}):
a = price.text.strip()
stock.append(a)
fill()
populate()
else:
for price in soup.find_all('span',{"class" : "tbox-list-button-sell"}):
sell[rep1] = (price.text)
rep1 = rep1 + 1
for price in soup.find_all('span',{"class" : "tbox-list-button-buy"}):
buy[rep2] = (price.text)
rep2 = rep2 + 1
populate()
connection = sqlite3.connect("stocks.db")
cursor = connection.cursor()
web = ["C:/Users/Luke_2/Desktop/Computing/Coursework/Practice/Stocks1/demo.trading212.com.html","C:/Users/Luke_2/Desktop/Computing/Coursework/live/Stocks1/demo.trading212.com1.html","C:/Users/Luke_2/Desktop/Computing/Coursework/live/Stocks1/demo.trading212.com10.24.html"]
stock=[]
sell = []
buy = []
def run():
for i in web:
soup = BeautifulSoup(open(i),"html.parser")
get_stocks(soup)
run()
connection.commit()
connection.close()
答案 0 :(得分:1)
你没有告诉open()函数在阅读文件时使用什么编解码器,例如如果您在文件中使用utf-8:
在Python 2.7中:
import io
...
def run():
for i in web:
with io.open(i, encoding='utf-8') as infile:
soup = BeautifulSoup(infile,"html.parser")
对于Python3
def run():
for i in web:
with open(i, encoding='utf-8') as infile:
soup = BeautifulSoup(infile,"html.parser")