gpt4 book ai didi

php - 使用php获取外部html页面onclick内容

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:03 24 4
gpt4 key购买 nike

好吧,假设我有一个这样的 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')">

我想做的是针对每个 http://example.htm?some/link&id=*************** 我只想显示它们从外部 html 页面我尝试了下面的代码

$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$onclicks = $xpath->evaluate("/html/body//div");

for ($i = 0; $i < $onclicks->length; $i++) {
$onclick = $onclicks->item($i);
$display = $onclick->getAttribute("onclick");
echo $display."<br>";
}

它得到了这个

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')

任何想法如何得到我所追求的而不是同时点击内容任何答案将不胜感激。

最佳答案

我只使用 preg_match_all 而不是最终会因解析网站的 HTML 错误而失败的复杂 dom 解析。 .

这很可能更快并且更简单

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

enter image description here

关于php - 使用php获取外部html页面onclick内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com