【问题标题】:Using lxml to parse namepaced HTML?使用 lxml 解析命名空间的 HTML?
【发布时间】:2015-06-16 09:40:16
【问题描述】:

这让我彻底疯了,我已经为此苦苦挣扎了好几个小时。任何帮助将非常感激。

我正在使用PyQuery 1.2.9(构建在lxml 之上)来抓取this URL。我只想获取.linkoutlist 部分中所有链接的列表。

这是我的完整要求:

response = requests.get('http://www.ncbi.nlm.nih.gov/pubmed/?term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care')
doc = pq(response.content)
links = doc('#maincontent .linkoutlist a')
print links

但这会返回一个空数组。如果我改用这个查询:

links = doc('#maincontent .linkoutlist')

然后我得到这个 HTML:

<div xmlns="http://www.w3.org/1999/xhtml" xmlns:xi="http://www.w3.org/2001/XInclude" class="linkoutlist">
   <h4>Full Text Sources</h4>
   <ul>
      <li><a title="Full text at publisher's site" href="http://meta.wkhealth.com/pt/pt-core/template-journal/lwwgateway/media/landingpage.htm?issn=0268-1315&amp;volume=19&amp;issue=3&amp;spage=125" ref="itool=Abstract&amp;PrId=3159&amp;uid=15107654&amp;db=pubmed&amp;log$=linkoutlink&amp;nlmid=8609061" target="_blank">Lippincott Williams &amp; Wilkins</a></li>
      <li><a href="http://ovidsp.ovid.com/ovidweb.cgi?T=JS&amp;PAGE=linkout&amp;SEARCH=15107654.ui" ref="itool=Abstract&amp;PrId=3682&amp;uid=15107654&amp;db=pubmed&amp;log$=linkoutlink&amp;nlmid=8609061" target="_blank">Ovid Technologies, Inc.</a></li>
   </ul>
   <h4>Other Literature Sources</h4>
   ...
</div>

所以父选择器确实返回带有大量&lt;a&gt; 标记的HTML。这似乎也是有效的 HTML。

更多的实验表明,出于某种原因,lxml 不喜欢开头 div 上的xmlns 属性。

如何在 lxml 中忽略它,像普通 HTML 一样解析它?

更新:尝试ns_clean,仍然失败:

    parser = etree.XMLParser(ns_clean=True)
    tree = etree.parse(StringIO(response.content), parser)
    sel = CSSSelector('#maincontent .rprt_all a')
    print sel(tree)

【问题讨论】:

    标签: python html html-parsing lxml pyquery


    【解决方案1】:

    您需要处理命名空间,包括一个空的。

    工作解决方案:

    from pyquery import PyQuery as pq
    import requests
    
    
    response = requests.get('http://www.ncbi.nlm.nih.gov/pubmed/?term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care')
    
    namespaces = {'xi': 'http://www.w3.org/2001/XInclude', 'test': 'http://www.w3.org/1999/xhtml'}
    links = pq('#maincontent .linkoutlist test|a', response.content, namespaces=namespaces)
    for link in links:
        print link.attrib.get("title", "No title")
    

    打印与选择器匹配的所有链接的标题:

    Full text at publisher's site
    No title
    Free resource
    Free resource
    Free resource
    Free resource
    

    或者,只需将 parser 设置为 "html" 并忘记命名空间:

    links = pq('#maincontent .linkoutlist a', response.content, parser="html")
    for link in links:
        print link.attrib.get("title", "No title")
    

    【讨论】:

    • 非常感谢。出于兴趣,你能告诉我为什么我看到这个命名空间附加到 div 元素上吗?它不在页面的源代码中。
    • @Richard 很好的问题,这让我认为命名空间是由 pyquery 插入的,因为它尝试使用 xml 解析器解析内容,而需要通过 html 解析器进行解析,请参阅更新。希望对您有所帮助。
    【解决方案2】:

    祝你好运,让标准的 XML/DOM 解析能够在大多数 HTML 上工作。您最好的选择是使用BeautifulSouppip install beautifulsoup4easy_install beautifulsoup4),它对错误构建的结构有很多处理。也许是这样的?

    import requests
    from bs4 import BeautifulSoup
    
    response = requests.get('http://www.ncbi.nlm.nih.gov/pubmed/?term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care')
    bs = BeautifulSoup(response.content)
    div = bs.find('div', class_='linkoutlist')
    links = [ a['href'] for a in div.find_all('a') ]
    
    >>> links
    ['http://meta.wkhealth.com/pt/pt-core/template-journal/lwwgateway/media/landingpage.htm?issn=0268-1315&volume=19&issue=3&spage=125', 'http://ovidsp.ovid.com/ovidweb.cgi?T=JS&PAGE=linkout&SEARCH=15107654.ui', 'https://www.researchgate.net/publication/e/pm/15107654?ln_t=p&ln_o=linkout', 'http://www.diseaseinfosearch.org/result/2199', 'http://www.nlm.nih.gov/medlineplus/antidepressants.html', 'http://toxnet.nlm.nih.gov/cgi-bin/sis/search/r?dbs+hsdb:@term+@rn+24219-97-4']
    

    我知道这不是您想要使用的库,但在谈到 DOM 时,我曾多次将头撞到墙上。 BeautifulSoup 的创建者已经规避了许多在野外容易发生的极端情况。

    【讨论】:

      【解决方案3】:

      如果我没记错的话,我不久前自己也遇到过类似的问题。您可以通过将命名空间映射到 None 来“忽略”命名空间,如下所示:

      sel = CSSSelector('#maincontent .rprt_all a', namespaces={None: "http://www.w3.org/1999/xhtml"})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        相关资源
        最近更新 更多