python 爬取 KeyError

 幻想6666_321 发布于 2022-11-26 14:54
from html.parser import HTMLParser
import urllib.request
with urllib.request.urlopen('https://www.python.org/events/python-events/') as url:
    content = url.read()
html = content.decode('utf-8')

class MyHtmlParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.location = []
        self.flag = False

    def handle_starttag(self, tag, attrs):
        if tag == 'span' and 'event-location' in dict(attrs)['class']:
            self.flag = True

    def handle_data(self, data):
        if self.flag:
            self.location.append(data)
            self.flag = False
parser = MyHtmlParser()
parser.feed(html)
for i in parser.location:
    print(i)

运行结果:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/untitled1/python6.py", line 22, in 
    parser.feed(html)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\html\parser.py", line 111, in feed
    self.goahead(0)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\html\parser.py", line 171, in goahead
    k = self.parse_starttag(i)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\html\parser.py", line 345, in parse_starttag
    self.handle_starttag(tag, attrs)
  File "C:/Users/user/PycharmProjects/untitled1/python6.py", line 14, in handle_starttag
    if tag == 'span' and 'event-location' in dict(attrs)['class']:
KeyError: 'class'

为什么会有KeyError呢?这是爬取的目标网站的一段代码:

 Capital One McLean Conference Center in McLean, VA, USA

没有大小写错误,怎么会有KeyError呢?

另外我爬取这一段代码:

PyConES 2016 - Almeria

把代码改成if tag == 'a' and 'python-event' in dict(attrs)['href']: 
就可以成功爬取。。。
所以是什么问题呢?
是class有禁忌????

1 个回答
  • 如果某个span没有 class属性,dict就报错~

            if tag == 'span' and 'event-location' in dict(attrs)['class']:
                self.flag = True

    改成这样:

            if tag == 'span' and 'class' in dict(attrs) and 'event-location' in dict(attrs)['class']:
                self.flag = True


    2022-11-26 15:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有