Я все время пытаюсь выполнить следующий код, но получаю следующие сообщения об ошибках:
html = urllib.request.urlopen(url, context=ctx).read()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
'' '
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import sqlite3
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = "http://www.pythonlearn.com/code/mbox.txt"
html = urllib.request.urlopen(url, context=ctx).read()
fh = BeautifulSoup(html, 'html.parser')
#Connecting to the file in which we want to store our db
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
#Deleting any possible table that may affect this assignment
cur.execute('''
DROP TABLE IF EXISTS Counts''')
#Creating the table we're going to use
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
#Reading each line of the file
for line in fh:
#Finding an email address and splitting it into name and organization
if not line.startswith('From: ') : continue
pieces = line.split()
email = pieces[1]
(emailname, organization) = email.split("@")
print (email)
#Updating the table with the correspondent information
cur.execute('SELECT count FROM Counts WHERE org = ? ', (organization, ))
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts (org, count)
VALUES ( ?, 1 )''', ( organization, ) )
else :
cur.execute('UPDATE Counts SET count=count+1 WHERE org = ?',
(organization, ))
# We commit the changes after they've finished because this speeds up the
# execution and, since our operations are not critical, a loss wouldn't suppose
# any problem
conn.commit()
# Getting the top 10 results and showing them
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
print ("Counts:")
for row in cur.execute(sqlstr) :
print (str(row[0]), row[1])
#Closing the DB
cur.close()
' '' Я не уверен, что неверно.