【问题标题】:Parsing html using Xpath in python在python中使用Xpath解析html
【发布时间】:2015-04-29 10:01:13
【问题描述】:

我有一个下面的 html,我试图使用 xpath 解析它。但我只会得到空刺作为回报。谁能告诉我我错在哪里。我已经尝试了一切,但无法成功。

标签的 Xpath 代码:

divLbl=ch.xpath("//div[@class='left-container']/article/ul[@class='list-unstyled row']/li[@class='col-sm-6 mrg-bottom']/span[@class='text-light']")

对应标签值的Xpath代码:

divVal=ch.xpath("//div[@class='left-container']/article/ul[@class='list-unstyled row']/li[@class='col-sm-6 mrg-bottom']/span[@class='text-light']/strong")

HTML 值:

<div>
                        <h2 class="rowbreak"><strong>Information of the Car</strong></h2>
                        <ul class=" list-unstyled row">
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-calendar text-light"></span> <span class=" text-light">Make Year:</span> <strong>Aug 2009</strong></li>
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-road text-light"></span> <span class=" text-light">Kilometers:</span> <strong>127,553</strong></li>
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-map-marker text-light"></span> <span class=" text-light">City:</span> 
                                <strong class="carCity_795606">  
                                                                        <a href="javascript:void(0);" onclick="javascript: $( &quot;#maplinkbtn&quot; ).trigger( &quot;click&quot; ); ">
                                    Sambalpur                                    </a>
                                                                    </strong>

                            </li>
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-calendar text-light"></span> <span class=" text-light">Listing Date:</span> <strong>27 Apr 2015</strong></li>
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-user text-light"></span> <span class=" text-light">No. of Owners:</span> <strong> First Owner</strong>
                            </li>
                            <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-tint text-light"></span> <span class=" text-light">Fuel Type:</span> <strong> Petrol</strong></li>
                              <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-user text-light"></span> <span class=" text-light">Posted by:</span> <strong> 
                                  Dealer</strong>
                            </li>
                        </ul>
           </div>

编辑的 HTML:

 <div>
                    <h2 class="rowbreak"><strong>Information of the Car</strong></h2>
                    <ul class=" list-unstyled row">
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-calendar text-light"></span> <span class=" text-light">Make Year:</span> <strong>Aug 2009</strong></li>
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-road text-light"></span> <span class=" text-light">Kilometers:</span> <strong>127,553</strong></li>
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-map-marker text-light"></span> <span class=" text-light">City:</span> 
                            <strong class="carCity_795606">  
                                                                    <a href="javascript:void(0);" onclick="javascript: $( &quot;#maplinkbtn&quot; ).trigger( &quot;click&quot; ); ">
                                Sambalpur                                    </a>
                                                                </strong>

                        </li>
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-calendar text-light"></span> <span class=" text-light">Listing Date:</span> <strong>27 Apr 2015</strong></li>
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-user text-light"></span> <span class=" text-light">No. of Owners:</span> <strong> First Owner</strong>
                        </li>
                        <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-tint text-light"></span> <span class=" text-light">Fuel Type:</span> <strong> Petrol</strong></li>
                          <li class="col-sm-4 mrg-bottom"><span class="glyphicon glyphicon-user text-light"></span> <span class=" text-light">Posted by:</span> <strong> 
                              Dealer</strong>
                        </li>
                    </ul>
       </div>

 <h2 class="rowbreak"></h2>
    <ul class=" list-unstyled row">
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light">One Time Tax :</span> <strong>Individual</strong></li>
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light">Registration No. :</span> <strong>OR03F3141</strong></li>
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light"> Insurance &amp; Expiry :</span> <strong>No Insurance&nbsp;</strong></li>
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light">Registration Place: </span> <strong> Sambalpur</strong></li>
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light">Transmission :</span> <strong>Manual</strong></li>
                            <li class="col-sm-6 mrg-bottom"><span class=" text-light">Color :</span> <strong>Silver</strong></li>
                        </ul>

【问题讨论】:

    标签: python html xpath html-parsing


    【解决方案1】:

    您当前使用的 XPath 非常脆弱 - 您正在检查链中的每个元素并使用“面向布局”的类。

    我将从包含 strong 元素和“汽车信息”文本的 h2 元素开始,并获得以下 ul 元素。例如。获取所有标签:

    //h2[strong = 'Information of the Car']/following-sibling::ul/li/span/text()
    

    演示:

    In [3]: ch = fromstring(data)
    
    In [4]: ch.xpath("//h2[strong = 'Information of the Car']/following-sibling::ul/li/span/text()")
    ['Make Year:', 'Kilometers:', 'City:', 'No. of Owners:', 'Fuel Type:', 'Posted by:']
    

    示例(获取名称和值):

    In [25]: for field in ch.xpath("//h2/following-sibling::ul/li"):
        name = ''.join(field.xpath(".//span/text()")).strip()
        value = ''.join(field.xpath(".//strong//text()")).strip()
        print name, value
       ....:     
    Make Year: Aug 2009
    Kilometers: 127,553
    City: Sambalpur
    Listing Date: 27 Apr 2015
    No. of Owners: First Owner
    Fuel Type: Petrol
    Posted by: Dealer
    One Time Tax : Individual
    Registration No. : OR03F3141
    Insurance & Expiry : No Insurance
    Registration Place: Sambalpur
    Transmission : Manual
    Color : Silver
    

    【讨论】:

    • 完全同意“面向布局”的课程+1
    • 谢谢alecxe。但是面向布局的类有什么问题。为什么不能使用 xpath 解析它们?你能详细说明一下吗?
    • @Naresh 当然,这里的重点是,例如 col-sm-6 之类的类用于页面布局、设计和容器/框大小 - 这些是可能会改变的东西未来会导致您的代码中断。此外,使用这样的类不会使您的表达式具有可读性和简洁性。
    • @Naresh 尽可能将内容与演示文稿分开。如果您使用布局说明来查找内容,那么对布局的任何更改都会阻止您查找内容 - 即使它仍然存在。
    • @Naresh 当然,我使用了更新的 html sn-p 并提供了一个示例代码来从两个 uls 中获取名称和值,检查一下。希望对您有所帮助。
    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2015-07-28
    相关资源
    最近更新 更多