Python bs4 Неизвестная ошибка: "return self.attrs [key] KeyError: 'id'" - PullRequest
0 голосов
/ 24 марта 2020

Я экспериментирую с python чисткой веб-страниц. Я следую учебному пособию @ https://realpython.com/python-web-scraping-practical-introduction/ и обнаружил сообщение об ошибке, которое на самом деле не понимаю.

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/bs4/element.py", line 1321, in __getitem__
    return self.attrs[key]
KeyError: 'id'

Код ошибки появился после того, как я запустил следующий код в своем терминале enter image description here

, где mathematicians.py является следующим:

from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup

def simple_get(url):
    try:
        with closing(get(url, stream=True)) as resp:
            if is_good_response(resp):
                return resp.content
            else:
                return None
    except RequestExeception as e:
        log_error("Error during requests to {0} : {1}".format(url, str(e))) #prints the error msg lol
        return None

def is_good_response(resp):
    content_type = resp.headers['Content-Type'].lower() #converts all headers to lowercase
    return (resp.status_code == 200 and content_type is not None and content_type.find('html') >-1)
    #return the status_code and that content is not empty as well as finding the word html??

def log_error(e):
    print(e)

Может кто-нибудь помочь объяснить в чем ошибка? (Я читал в Интернете, это связано с отсутствием тега id? Но я все еще в замешательстве ...)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...