【问题标题】:scrape the data from html page php从 html 页面 php 中抓取数据
【发布时间】:2011-03-23 02:43:30
【问题描述】:

我需要从 html 页面中抓取数据

<div style="margin-top: 0px; padding-right: 5px;" class="lftFlt1">

    <a href="" onclick="setList1(157204);return false;" class="contentSubHead" title="USA USA">USA USA</a>
    <div style="display: inline; margin-right: 10px;"><a href="" onclick="rate('157204');return false;"><img src="http://icdn.raaga.com/3_s.gif" title="RATING: 3.29" style="position: relative; left: 5px;" height="10" width="60" border="0"></a></div>
    </div>

我需要从onclick="setList1...刮掉“USA USA”157204...

【问题讨论】:

  • 你要报废还是报废?保留数据还是保留 HTML 并丢弃数据?
  • @relet:好问题。我经常看到这个拼写错误,所以我编辑为“scrape”。 @Ram:如果“废品”确实是您的意图,请回滚。
  • 是我的错我很抱歉
  • Scrape web page contents的可能重复

标签: php html


【解决方案1】:

您应该使用DOMDocumentXPath。通常不建议使用 RegEx 来解析 HTML。

【讨论】:

    【解决方案2】:

    使用正则表达式:

    /setList1\(([0-9]+)\)[^>]+title="([^"]+)"/si
    

    和 preg_match() 或 preg_match_all()

    【讨论】:

    【解决方案3】:

    请转至through my previous answers about how to handle HTML with DOM

    XPath 获取所有锚元素的文本内容:

    //a/text()
    

    XPath 获取所有锚元素的title属性:

    //a/@title
    

    XPath 获取所有锚元素的 onclick 属性:

    //a/@onclick
    

    您将不得不使用一些字符串函数从点击文本中提取数字。

    【讨论】:

      【解决方案4】:

      到目前为止,最好的抓取库是简单的 html dom。基本上使用jquery选择器语法。

      http://simplehtmldom.sourceforge.net/

      您在本例中获取数据的方式:

      include("simple_html_dom.php");
      $dom=str_get_html("page.html");
      $text=$dom->find(".lftFlt1 a.contentSubHead",0)->plaintext;
      //or 
      $text=$dom->find(".lftFlt1 a.contentSubHead",0)->title;
      

      【讨论】:

      • 这是你的意见。您尝试了多少其他库?实际使用 DOM 而不是字符串解析的建议第三方替代方案:phpQueryZend_DomFluentDom
      • 是的,它对我有帮助,但我更改了一个更正 $dom=file_get_html("page.html");你能解释一下.lftFlt1 a.contentSubHead这个东西吗???
      • @Ram 这是一个 CSS 选择器,它(与史蒂夫的建议相反)不是 jQuery,而是 W3C 标准:w3.org/TR/CSS2/selector.html
      • 好吧..但我在网页中使用 lftFlt1 有更多课程我使用此代码它不起作用 $dom=file_get_html("raaga.com/channels/tamil/moviedetail.asp?mid=T0001923"); foreach($text=$dom-> find(".lftFlt1 a.contentSubHead",0) as $a) { echo $a->plaintext; }
      • @Ram 显然。 find 的第二个参数返回第 n 个孩子。请参阅文档simplehtmldom.sourceforge.net/manual.htm
      【解决方案5】:

      我是这样做的

      $a=$coll->find('div[class=lftFlt1]');
      $text=$element->find("a[class=cursor]",0)->onclick;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        • 2015-07-20
        • 2021-05-18
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多