【问题标题】:How to extarct element in html page如何在html页面中提取元素
【发布时间】:2017-11-20 13:10:30
【问题描述】:

我将通过 PHP 从 HTML 网页中提取特定信息。例如,在我不知道如何提取<span class="fpStriked">....</span> 之间的所有值之后。

$url = 'https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=ordinateur';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);

$myarray = array($html);

【问题讨论】:

  • document.getElementsByClassName("fpStriked").innerHTML ?
  • @UsmanRana getElementsByClassName 返回一个集合
  • 在 php 或使用 javascript 的客户端中提取它...用它做什么?问题太宽泛,不包括样本输入或预期结果。花点时间阅读How to Askminimal reproducible example
  • 另外,请在询问之前做一些研究。如何使用任何一种语言解析 html 都可以轻松搜索
  • 是的。我只是虽然 OP 想要提到的元素中的所有内容。我希望他会知道如何循环获取相关的东西

标签: php jquery html


【解决方案1】:

如果要提取任意标签内的所有文本,最简单的方法是剥离标签:strip_tags()

如果您想删除特定标签,也许this SO questions 会有所帮助。

您可能想看看PHP Simple HTML DOM Parser 或类似的:

例如:

// Create DOM from URL or file
$html = str_get_html($html);

或者如果你想在客户端使用 JavaScript 做同样的事情,那么你可以使用:

document.getElementsByClassName("fpStriked").innerHTML

猜你喜欢
  • 2014-02-12
  • 2021-03-13
  • 2011-01-02
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2013-09-25
相关资源
最近更新 更多