【问题标题】:Parsing html in Python 2.7 with regex - don't really understand that使用正则表达式在 Python 2.7 中解析 html - 不太明白
【发布时间】:2012-12-12 06:53:03
【问题描述】:

抱歉有点笨,但我真的需要 Python 方面的帮助。

['<a href="needs to be cut out">Foo to BAR</a>', '<a href="this also needs to be cut out">BAR to Foo</a>']

所以我有这个元组,我需要删除该 href 属性内的内容以及 &lt;a&gt; 标记内的内容 - 基本上,我想要一个看起来像这样的元组:

[["needs to be cut out", "Foo to BAR"], ["this also needs to be cut out", "BAR to Foo"]]

在href属性里面有很多特殊符号,比如

<a href="?a=p.stops&amp;direction_id=23600&amp;interval=1&amp;t=wml&amp;l=en">

我认为,如果我真的不需要尝试解析对象树而只需要网页中的几个 url 和单词,那么使用 HTML 解析器会很麻烦。但我真的不明白如何形成正则表达式。我形成的正则表达式似乎完全错误。所以我在问是否有人可以帮助我。

【问题讨论】:

    标签: html regex string parsing python-2.7


    【解决方案1】:

    无论如何,只需使用 HTML 解析器。 Python 附带了一些,xml.etree.ElementTree API 比正则表达式更容易使用,即使是简单的带有任意属性的&lt;a&gt; 标签:

    from xml.etree import ElementTree as ET
    
    texts = []
    for linktext in linkslist:
        link = ET.fromstring(linktext)
        texts.append([link.attrib['href'], link.text])
    

    如果你使用' '.join(link.itertext()),你可以从嵌套在&lt;a&gt;标签下的anything中取出文本,如果你发现有些链接嵌套了&lt;span&gt;&lt;b&gt;&lt;i&gt; 或其他内联标签以进一步标记链接文本:

    for linktext in linkslist:
        link = ET.fromstring(linktext)
        texts.append([link.attrib['href'], ' '.join(link.itertext())])
    

    这给出了:

    >>> from xml.etree import ElementTree as ET
    >>> linkslist = ['<a href="needs to be cut out">Foo to BAR</a>', '<a href="this also needs to be cut out">BAR to Foo</a>']     
    >>> texts = []
    >>> for linktext in linkslist:
    ...     link = ET.fromstring(linktext)
    ...     texts.append([link.attrib['href'], ' '.join(link.itertext())])
    ... 
    >>> texts
    [['needs to be cut out', 'Foo to BAR'], ['this also needs to be cut out', 'BAR to Foo']]
    

    【讨论】:

      【解决方案2】:

      您可以使用 BeautifulSoup 来解析 HTML 实体。

      根据您的问题,您已经有以下列表:

      l = ['<a href="needs to be cut out">Foo to BAR</a>', '<a href="this also needs to be cut out">BAR to Foo</a>']
      

      现在您只需要以下代码。

      from BeautifulSoup import BeautifulSoup
      
      parsed_list = []
      
      for each in l:
          soup = BeautifulSoup(each)
          parsed_list.append([soup.find('a')['href'], soup.find('a').contents[0]])
      

      希望对你有帮助:)

      【讨论】:

        【解决方案3】:

        我会为此使用 Easy Html Parser EHP。

        查看https://github.com/iogf/ehp

        lst = ['<a href="needs to be cut out">Foo to BAR</a>', '<a href="this also needs to be cut out">BAR to Foo</a>', '<a href="?a=p.stops&amp;direction_id=23600&amp;interval=1&amp;t=wml&amp;l=en">']
        
        data = [(tag.text(), attr.get('href'))for indi in lst
                    for tag, name, attr in Html().feed(indi).walk() if attr.get('href')]
        
        
        data
        

        输出:

        [('Foo to BAR', 'needs to be cut out'), ('BAR to Foo', 'this also needs to be cut out'), ('', u'?a=p.stops&direction_id=23600&interval=1&t=wml&l=en')]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-06-26
          • 1970-01-01
          • 2012-09-12
          • 2014-06-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多