2
votes
def get_stock(item_url):
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, features="html.parser")

    for link in soup.findAll('a',{'class':'none'}):
        words=link.string
        stock_num=words[1:5]


        if stock_num.isdigit():
            href='https://tw.stock.yahoo.com/q/q?s='+ stock_num
            print(stock_num)
            c.execute('insert into stocks(stocknum) values (?)',stock_num)
            conn.commit()

I'm trying to insert four-digit strings stock_num into my SQLite. However, it shows

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.

Below is my code in creating table

CREATE TABLE "stocks" (
    "stocknum"  TEXT NOT NULL,
    PRIMARY KEY("stocknum")
);

I couldn't figure out how to adjust my table or the crawler. I've been at it for hours and I cannot figure out what's going on.

1

1 Answers

4
votes

You will need to pass the parameters in a tuple – an 1-tuple if there is only one, but a tuple nonetheless.

The error message stems from the string stock_num happening to have 4 digits, so the sqlite wrapper parses e.g. "1337" as "1", "3", "3", "7".

To fix this,

c.execute('insert into stocks(stocknum) values (?)', stock_num)

should be

c.execute('insert into stocks(stocknum) values (?)', (stock_num,))