选择相同表行上的URL作为不同文本

时间:2017-10-13 16:26:19

标签: xml xpath xhtml

我有一张我想要刮的桌子,看起来如下:

https://i.imgur.com/Hlemt1y.jpg

以下是表格中一行的HTML:

<TR >

            <script>
                  if (document.getElementById("Function").value != 'Customer')
                            document.write('<td align="center">CO</td>');</script>
            <td align="left"><a href="OrdDetList.pgm?Order=8M216&Purpose=Customer&ShowPrice=&OpenOnly=Y">8M216 </a></td>
             <script> if (document.getElementById("Function").value != 'Customer')
                            document.write('<td align="center">R</td> <td align="center">O</td>');</script>
                            <td align="center">Backordered</td>
            <td align="left"><a href="OrdersList.pgm?Customer=33333&CompDiv=all&Function=Customer&OpenOnly=Y&ShowPrice= &YearsBack=      ">70036</a>
                    <a class=info href="#"><img src="../images/help.gif" border=none>
                        <span>
                        <div id="SoldToNameAddress">
                    123 our address<br>
                        </div>
                        </span>
                            </a>
                    </td>
            <td align="left">our company</td>
            <td align="left"><a href="OrdersList.pgm?Customer=33333&CompDiv=all&Function=Customer&OpenOnly=Y&ShowPrice= &YearsBack=      ">70037</a>
                    <a class=info href="#"><img src="../images/help.gif" border=none>
                        <span>
                        <div id="ShipToNameAddress">
                    our address
                        </div>
                        </span>
                            </a>
                    </td>
            <td align="left">our company name</td>
            <td align="left">70037</td>
            <td align="left">052317</td>
        <script>
                  if (document.getElementById("Function").value != 'Customer')
                            document.write('<td align="center">3</td>');</script>
            <td align="left"><a class=info href="#">17/05/23<span>May 23, 2017        </span></a></td>
            <td align="left"><a class=info href="#">17/05/23<span>May 23, 2017        </span></a></td>
            <td align="center"></td>

        </TR>

我的目标是选择网址:

<a href="OrdDetList.pgm?Order=8M216&Purpose=Customer&ShowPrice=&OpenOnly=Y">8M216 </a>

搜索我们的采购订单时<td align="left">052317</td>

我是使用Xpath的新手,所以我能够到目前为止能够通过搜索8M216直接获取URL。但我不知道如何使用XPATH根据其他表格单元格中的PO 052317给出其他URL。

到目前为止,这是我的代码,但由于我上面所说的内容,它的用处无用:

<?php

$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);

$html = file_get_contents('https://thewebsiteiamscraping', false, stream_context_create($arrContextOptions)); //get the html returned from the following url

$order_doc = new DOMDocument();

libxml_use_internal_errors(TRUE); //disable libxml errors

if(!empty($html)){ //if any html is actually returned

    $order_doc->loadHTML($html);
    libxml_clear_errors(); //remove errors for yucky html

    $order_xpath = new DOMXPath($order_doc);

    //get order URLS based on our PO#
    $order_row = $order_xpath->query('//a[text()="8M216 "]/@href');

    if($order_row->length > 0){
        foreach($order_row as $row){
            echo $row->nodeValue . "<br/>";
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

  

我认为这个XPath指令将帮助您找到正确的XPath

enter image description here

您可以在Chrome中打开控制台,然后输入$x("your_xpath_here")来检查XPath。这将返回一组匹配的值。如果它为空,则表示页面上没有匹配项。