V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
15874103329
V2EX  ›  Python

如何解决 KeyError: 'name'

  •  
  •   15874103329 · 2018-11-04 11:46:17 +08:00 · 4000 次点击
    这是一个创建于 1999 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import re
    from urllib import request


    class Spider():

    url = 'https://www.panda.tv/cate/lol?pdt=1.24.s1.3.15r0uaprii3'

    root_pattern = ('<div class="video-info">([\s\S]*?)</div>')
    name_pattern = ('</i>([\s\S]*?)</span>')
    number_pattern = ('<span class="video-number">([\s\S]*?)</span>')

    def __fetch_content(self):
    r = request.urlopen(Spider.url)
    htmls = r.read()
    htmls = str(htmls,encoding='utf-8')
    return htmls

    def __analysis(self,htmls):
    root_html = re.findall(Spider.root_pattern,htmls)
    anchors = []
    for html in root_html:
    name = re.findall(Spider.name_pattern,html)
    nember = re.findall(Spider.number_pattern,html)
    anchor = {'nema':name,'nember':nember}
    anchors.append(anchor)

    return anchors

    def __refine(self,anchors):
    l = lambda anchor: {
    'name':anchor['name'][0].strip(),
    'nember':anchor['nember'][0]
    }
    return map(l , anchors)

    def __sort(self,anchors):
    anchors = sorted(anchors,key=self.__sort_seed)
    return anchors

    def __sort_seed(self,anchor):
    return anchor['nember']

    def __show(self,anchors):
    for anchor in anchors:
    print(anchor['name'] + '————' + anchor['nember'])

    def go(self):
    htmls = self.__fetch_content()
    anchors = self.__analysis(htmls)
    anchors = list(self.__refine(anchors))
    anchors = self.__sort(anchors)
    self.__show(anchors)



    spider = Spider()
    spider.go()
    12 条回复    2018-11-05 13:44:48 +08:00
    0312birdzhang
        1
    0312birdzhang  
       2018-11-04 11:59:43 +08:00   ❤️ 2
    'name':anchor['name'][0].strip()

    没有 name 这个键值呗,你的程序需要多做一些异常判断。
    没仔细看,md 是支持代码格式化的,你这样贴上来都没用看的欲望了
    15874103329
        2
    15874103329  
    OP
       2018-11-04 12:02:38 +08:00
    我刚刚学纯小白,老哥能加个 QQ 或者微信么,我截了图,这里还不能发图
    laike9m
        3
    laike9m  
       2018-11-04 12:03:56 +08:00 via Android
    @15874103329 人家都给你解释得这么明白了,就没必要加 qq 微信打扰了吧
    0312birdzhang
        4
    0312birdzhang  
       2018-11-04 12:04:31 +08:00
    @15874103329 #2 不能,markdown 了解一下,不能发图我这个 doge 是哪里来的

    爬虫的话用 bs4 或者其他的更方便
    hsfzxjy
        5
    hsfzxjy  
       2018-11-04 12:20:45 +08:00 via Android
    你的 name 拼成 nema 了
    gstqc
        6
    gstqc  
       2018-11-04 12:43:19 +08:00 via Android
    这几天看到两个拼写错误的帖子了😂😂😂
    Kylin30
        7
    Kylin30  
       2018-11-04 13:23:46 +08:00   ❤️ 1
    建议每次有拼写错误或者不细心又犯懒的时候就用力扇自己一耳光,可以加深记忆。
    15874103329
        8
    15874103329  
    OP
       2018-11-04 13:32:50 +08:00
    沃日,真的是单词错了,已经狠狠扇了自己两巴掌,感谢各位老哥了
    likuku
        9
    likuku  
       2018-11-04 14:12:49 +08:00
    以作伸手党为耻...
    luomu24
        10
    luomu24  
       2018-11-04 14:12:58 +08:00
    啪啪啪,再帮你来两下。/doge
    Rehtt
        11
    Rehtt  
       2018-11-04 15:37:45 +08:00 via Android
    建议使用一些代码校验的插件
    Ken999
        12
    Ken999  
       2018-11-05 13:44:48 +08:00
    建议你用 pycharm,可以有单词拼写检查,让英语小白少出一些错!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3305 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:50 · PVG 20:50 · LAX 05:50 · JFK 08:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.