Xpath 如何拉href链接

Xpath 如何拉href链接,xpath,Xpath,我正试图从一个正式的页面中提取一个链接,我似乎无法通过简单的谷歌搜索找到这个链接。。。这可能很简单,但xpath不是我的专业领域 我正在使用c#并尝试拉取链接并将其写入控制台,以了解如何获取链接 这是我的C代码 html代码部分是 <div class="last">&hellip;</div><a href="/cat/vehicles/cars/0_-_4_years_old/?p=13">13</a><a href="/cat/

我正试图从一个正式的页面中提取一个链接,我似乎无法通过简单的谷歌搜索找到这个链接。。。这可能很简单,但xpath不是我的专业领域

我正在使用c#并尝试拉取链接并将其写入控制台,以了解如何获取链接

这是我的C代码

html代码部分是

<div class="last">&hellip;</div><a href="/cat/vehicles/cars/0_-_4_years_old/?p=13">13</a><a href="/cat/vehicles/cars/0_-_4_years_old/?p=2">&raquo;</a>
<select name="sortby" class="sortby" onchange="doSort(this);">
    <option value="">Most Recent</option>
    <option value="of" >Oldest First</option>
    <option value="mw" >Most Views</option>
    <option value="lw" >Fewest Views</option>
    <option value="lp" >Lowest Price</option>
    <option value="hp" >Highest Price</option>
</select><div style="clear:both"></div>
</div>
<br /><br /><br />
                <a href="/details/2008_vw_gti/1454282/" class="prod_container" >
        <h2>2008 VW GTi</h2>
        <div style="float:left; width:122px; z-index:1000">
        <div class="thumb"><img src="http://c.castanet.net/img/28/thumbs/1454282-1-1.jpg" border="0"/></div>
        <div class="clear"></div>
        mls
        </div>
        <div class="descr">
            The most fun car I have owned.  Dolphin Grey, 4 door, Dual Climate control, DRG Transmission with paddle shift.  Leather...
        </div>
        <div class="pdate">
            <p class="price">$19,000.00</p>
            <p class="date">Kelowna<br />Posted: Oct 15,  2:54 PM<br />Views: 349</p>
        </div>
        <div style="clear:both" ></div>
        <div class="seal"><img src="/images/bookmark.png" /></div>
        </a>
                <a href="/details/price_drop_gorgeous_rare_white_2009_honda_accord_ex-l_coupe/1447341/" class="prod_container" >
        <h2>PRICE DROP!!! Gorgeous Rare White 2009 Honda Accord EX-L Coupe </h2>
        <div style="float:left; width:122px; z-index:1000">
        <div class="thumb"><img src="http://c.castanet.net/img/28/thumbs/1447341-1-1.jpg" border="0"/></div>
        <div class="clear"></div>
        sun2010
        </div>
        <div class="descr">
&hellip;
最近的
老一辈
大多数观点
最少视图
最低价格
最高价格



价格下降!!!华丽稀有白色2009本田雅阁EX-L双门轿跑车 2010年太阳

我想得到的链接是“/details/2008_vw_gti/1454282/”部分。谢谢

请尝试以下Xpath表达式:

//a[@class="prod_container"]/@href

HTML不是XML。 XPath是在XML文档中导航的工具,但是HTML不符合XML要求。您链接的HTML不是格式良好的XML,因此XPath不起作用


您需要查看如何使用HTML到XML转换器,然后将转换的输出添加到问题中,以编写XPath,或者使用其他工具进行调查。我建议在谷歌上搜索“C#HTML scrapers”,但我不熟悉.Net,所以我不能提供更窄的选择。

我在这里做了很多假设,因为你只提供了网站可能包含的一小部分内容,但是试试这个:
//a[@class=“prod#u container”]/@href
我已经编辑了原始帖子来添加更多细节。您提供的XPATH刚刚在控制台中返回了一个空值。如果工作正常,您可以测试它:
xmllint 2>/dev/null--html--XPATH'//a[@class=“prod_container”]/@href'http://stardust.hd.free.fr:8000
这是标签中的代码。。。
//a[@class="prod_container"]/@href