使用php在单击内容时获取外部html页面

使用php在单击内容时获取外部html页面,php,html,Php,Html,好吧,假设我有一个像这样的html文件 <div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=1357900324528'"> <div class="vad buttonDiv" onclick="other('example')"> <div class="vad buttonDiv" onclick="location.href='http:

好吧,假设我有一个像这样的html文件

<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=1357900324528'">
<div class="vad buttonDiv" onclick="other('example')">
<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=7458758375733'">
<div class="vad buttonDiv" onclick="other('example1')">
<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=3474537737392'">
<div class="vad buttonDiv" onclick="other('example2')">
它得到了这个

location.href='http://example.htm?some/link&id=1357900324528'
other('example')

location.href='http://example.htm?some/link&id=7458758375733
other('example1')

location.href='http://example.htm?some/link&id=3474537737392
other('example2')
任何关于如何获取我想要的内容的想法,而不是在点击内容上同时获得任何答案,我们都将不胜感激。

简单解决方案:

for ($i = 0; $i < $onclicks->length; $i++) {
    $onclick = $onclicks->item($i);
    $display = $onclick->getAttribute("onclick");
    if(substr($display, 0, 8) == 'location'){
        $display = str_replace(array("location.href='", "'"), '', $display);
        echo $display."<br>";
    }

}
for($i=0;$i<$onclicks->length;$i++){
$onclick=$onclicks->item($i);
$display=$onclick->getAttribute(“onclick”);
if(substr($display,0,8)=‘location’){
$display=str_replace(数组(“location.href=”、“”、“”)、$display);
echo$显示。“
”; } }
而不是复杂的dom解析,它最终会在我刚刚使用的解析网站的HTML错误上失败

这很可能更快,也不那么复杂

if ( preg_match_all( '/onclick="(location\\.href=([^"]+))"/i', $html, $matches ) )
{
    print_r( $matches );
}

你离成功太近了

在Wikipedia上学习了几分钟XPath之后,我想出了一个有效的XPath:

$html=<<<TEXT
<html>
<body>
<div>
<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=1357900324528'"></div>
<div class="vad buttonDiv" onclick="other('example')"></div>
<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=7458758375733'"></div>
<div class="vad buttonDiv" onclick="other('example1')"></div>
<div class="vad buttonDiv" onclick="location.href='http://example.htm?some/link&id=3474537737392'"></div>
<div class="vad buttonDiv" onclick="other('example2')"></div>
</div>
</body>
</html>
TEXT;
上述代码输出:

string(61) "location.href='http://example.htm?some/link&id=1357900324528'"
string(61) "location.href='http://example.htm?some/link&id=7458758375733'"
string(61) "location.href='http://example.htm?some/link&id=3474537737392'"
$url=”http://example.com";
$dom=新的DOMDocument();
@$dom->loadHTML($url);
$xpath=newdomxpath($dom);
$PATH=$xpath->evaluate('/html/body//div[@class=“vad buttonDiv”]');
对于($i=0;$i<$PATH->length;$i++){
$lmao=$PATH->item($i);
$answer=$lmao->getAttribute('onclick');
$searchArray=array(“location.href=”,“”);
$replaceArray=数组(“,”);
$link=str_replace($searchArray,$replaceArray,$answer);
echo$link。“
” }

显示只是链接。

不熟悉XPath,所以不确定,但您能使用类似于
“/html/body//div[onclick^=location]”的东西作为路径吗?Michel的解决方案非常有效,非常感谢您的回答
string(61) "location.href='http://example.htm?some/link&id=1357900324528'"
string(61) "location.href='http://example.htm?some/link&id=7458758375733'"
string(61) "location.href='http://example.htm?some/link&id=3474537737392'"
$url= "http://example.com";
$dom = new DOMDocument();
@$dom->loadHTML($url);
$xpath = new DOMXPath($dom);

$PATH = $xpath->evaluate('/html/body//div[@class="vad buttonDiv"]');
for ($i = 0; $i < $PATH->length; $i++) {
    $lmao = $PATH->item($i);

$answer = $lmao->getAttribute('onclick');
$searchArray = array( "location.href='", "'");
$replaceArray = array( "", "");
$link = str_replace($searchArray, $replaceArray, $answer);
echo $link."<br>"
}