python
  • python-2.7
  • xpath
  • html-parsing
  • 2016-05-06 41 views 1 likes 
    1

    我用下面的代码行从网页抓取CVE ID的:从xpath去除信息?

    project.cve_information = "".join(xpath_parse(tree, '//div[@id="references"]/a/text()')).split() 
    

    但是,问题是:

      <div id='references'> 
          <b>References:</b> 
          <a href='https://access.redhat.com/security/cve/CVE-2011-3256' target='_blank'>CVE-2011-3256&nbsp;<i class='icon-external-link'></i></a> 
          <a href='https://rhn.redhat.com/errata/RHSA-2011-1402.html' target='_blank'>RHSA-2011-1402&nbsp;<i class='icon-external-link'></i></a><br /> 
         </div> 
    

    参考:CVE-XXXX-XXXX RHSA-XXXX-XXXX

    如何避免RHSA和这些条目被解析?我只想要CVE-xxxx-xxxx值。我用它来提交表单是这样的:因为我的代码往往包括RHSA值

      "form[CVEID]" : ",".join(self.cve_information) if self.cve_information else "GENERIC-MAP-NOMATCH", 
    

    这种形式只执行对CVE值和错误的验证了。

    回答

    1

    您可以使用包含

    h = """ <div id='references'> 
          <b>References:</b> 
          <a href='https://access.redhat.com/security/cve/CVE-2011-3256' target='_blank'>CVE-2011-3256&nbsp;<i class='icon-external-link'></i></a> 
          <a href='https://rhn.redhat.com/errata/RHSA-2011-1402.html' target='_blank'>RHSA-2011-1402&nbsp;<i class='icon-external-link'></i></a><br /> 
         </div>""" 
    
    from lxml import html 
    
    xml = html.fromstring(h) 
    
    urls = xml.xpath('//div[@id="references"]/a[contains(@href, "CVE")]/@href') 
    

    或者,如果你想忽略与RHSA的HREF中,你可以使用不包含

    urls = xml.xpath('//div[@id="references"]/a[not(contains(@href, "RHSA"))]/@href') 
    

    两个会给你:

    ['https://access.redhat.com/security/cve/CVE-2011-3256'] 
    
    +0

    恩,我想我没有正确解释我的问题。我正在使用xpath表达式来解析“引用”字段。然后,我在其他地方使用“CVE-xxxx-xxxx”ID,以便它可以是https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-xxxx-xxxx。使用目前的解决方案,我得到 - 警告:无法找到有关CVE的信息CVE-2011-3256 - CVE-2011-3256 – Metahuman

    +0

    的额外“CVE”您想要CVE-2011-3256吗?如果它们总是在'/'末尾并且提取,如果它们可以在任何地方,那么你需要一个正则表达式或者分割并且使用str.strartswith来找到你想要的子字符串 –

    +0

    把xpath从'/ @ href '到'/ text()' –

    相关问题