【问题标题】:Replacement of Method has_key in Python3Python3中方法has_key的替换
【发布时间】:2018-09-15 22:51:15
【问题描述】:

我想改一下

def has_class_but_no_id(tag):
    return tag.has_key('class') and not tag.has_key('id')

此函数来自 Python2 而非 Python3

我想到了

我在这样的列表中更改了这个 HTML 文档

list_of_descendants = list(soup.descendants)

所以我可以获得包含类但不包含 id 的标签 这是关于找到所有带有class = blabla... 但不是id = .... 的标签 我不知道我该如何处理这个问题

【问题讨论】:

    标签: python python-3.x python-2.7 beautifulsoup web-crawler


    【解决方案1】:

    documentation 说:

    为了与 Python 3 兼容,我重命名了一种方法:

    • Tag.has_key() -> Tag.has_attr()

    另外,documentation here 中提供了完全相同的功能:

    如果其他匹配项都不适合您,请定义一个函数 将元素作为其唯一参数。该函数应返回True 如果参数匹配,则False 否则。

    这是一个函数,如果标签定义了“类”,则返回 True 属性但没有定义“id”属性:

    def has_class_but_no_id(tag):
        return tag.has_attr('class') and not tag.has_attr('id')
    

    【讨论】:

    • 好的,我使用了你的建议,但还有另一个问题 AttributeError: 'NavigableString' object has no attribute 'has_attr' 就像我说我将 Beautifulsoup 中的所有 Tag_elements 更改为 List 并且我应用了 has_attr 但是错误消息出来了,你可以给我更多建议吗?
    • 您正在对文本(NavigableString)和标签使用该功能。在文本上使用.has_attr 会引发上述错误。在调用该函数之前,请检查列表项是否为标签。您可以为此使用isinstance(item, Tag)。另外,不要忘记从 bs4 模块导入 Tag。
    • 如果您无法实现,请在问题中添加您正在尝试的代码,我会相应地编辑我的答案。
    【解决方案2】:

    嘿,我解决了这个问题。

    我要做的是

    1.收集所有标签(BeautifulSoup)和所有子标签(内容)

    soup = BeautifulSoup(html_doc,"html.parser")
    list_of_descendants = list(soup.descendants)
    

    2.消除所有 NavigableStrings(因为它们不能接受 has_attr() 方法)

    def terminate_navis(list_of_some):
    
        new_list = []
    
        for elem in list_of_some:
    
            if type(elem) == bs4.element.Tag:
                new_list.append(elem)
            else :
                continue
    
        return new_list 
    
    
    new_list = terminate_navis(list_of_descendants)
    
    
    def contents_adding(arg_list):
    //this Method helps that get all the childrens of tags in lists again
    
        new_list = arg_list
    
        child_list = []
    
        for elem in arg_list:
    
            if elem.contents:
    
                child_list = elem.contents
                child_list = terminate_navis(child_list)
                new_list.extend(child_list)
    
            new_list = list(set(new_list))
    
        return new_list
    

    3.过滤所有标签,如果他们有属性'class'(has_attr),如果他们没有'id'(也有has_attr)

    def justcl(tag_lists):
    
        class_lists = []
    
        for elem in tag_lists:
            if elem.has_attr('class'):
                class_lists.append(elem)
            else :
                continue
    
        return class_lists
    
    def notids(class_lists):
    
        no_id_lists = []
    
        for elem in class_lists:
    
            if elem.has_attr('id'):
                continue
            else :
                no_id_lists.append(elem)
    
        return no_id_lists
    
    1. 所有这些收集的标签都会创建为列表并打印在屏幕上

    打印或使用for循环等等...

    【讨论】:

    • 您能否格式化您答案中的所有代码,并用您想要完成的内容更新您的问题。具体来说,举个例子。我怀疑您是否需要答案中的所有这些凌乱的代码才能实现您的目标。
    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多