Answer a question

I'm a beginner at coding so sorry if I made any mistakes. I webscraped a website and got a list of tables from it. I'm trying to insert that data into sqlite 3 using a for loop, and it comes back as an error.

import requests
from bs4 import BeautifulSoup
import json
##from  tkinter import *
##bobert=Tk()
##bobert.geometry("600x600")
import sqlite3

##cursor.execute("CREATE TABLE IF NOT EXISTS covid (name STRING, passward STRING, score INTEGER)")
connection = sqlite3.connect('covidproject.db')
cursor = connection.cursor()


##cursor.execute("DROP TABLE IF EXISTS covid ")

##cursor.execute("CREATE TABLE IF NOT EXISTS covid (name STRING, confirmed REAL, changes_today REAL,deceased REAL,active REAL, recovered REAL)")

url = 'https://ncov2019.live/'
headers = {'User-Agent':'Mozilla/5.0'}
response = requests.get(url, headers = headers)
response.status_code

soup = BeautifulSoup(response.content,'html.parser')
stat_table = soup.find_all("table", attrs={"class": "display responsive"})

headers = [header.get_text(strip=True) for header in soup.find_all("th")]
rows = [dict(zip(headers, [td.get_text(strip=True) for td in row.find_all("td")]))
        for row in soup.find_all("tr")[1:-1]]

for i,x in enumerate(rows,9):
    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")
    connection.commit()

##print (json.dumps(rows[9], indent=2))

##row2=rows[9]
##print (rows[9]['Name'])
##print (rows[9])

here is the error:

Traceback (most recent call last):
  File "C:\Users\minio\Downloads\sqlite-tools-win32-x86-3310100\sqlite-tools-win32-x86-3310100\webscraping3.py", line 31, in <module>
    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")
KeyError: 'Name'

Answers

The KeyError: 'Name' error message is indicative that your dictionary does not have a 'Name' Key. You can do several thing to diagnose it;

The easiest thing to do first is to just check the base case, does it have the name entry. I.e.

print( rows[0]['Name'] ) 

If that works, the next thing you might want to do is to get better information on which row is failing, something like this might work:

for i,x in enumerate(rows,9):
    print( rows[i]['Name'] )
    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")
    connection.commit()

You can also step through your code with the Python debugger using a tutorial like https://realpython.com/python-debugging-pdb/. Alternatively, you can look at using a try/except to ignore a specific error.

Good luck!

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐