doubo1871 2012-07-20 11:59
浏览 106
已采纳

将特定区域的HTML从外部域网页加载到div中

I'm currently designing a website for a company that uses an external site to display information about its clients. Currently, their old website just puts a link to the external profile of each client. however with this rebuild, I wondered if there was any way to load a specific portion of the external site onto their new page.

I've done my research, and I've found it's possible using jQuery and AJAX (with a bit of a mod) but all the tutorials relate to a div tag being lifted from the external site then loaded into the new div tag on the page.

Here's my problem: after reviewing the source code of the external source, the line of HTML I want isn't contained in a named DIV (other than the master wrap and I can't load that!)

The tag I need is literally: <p class="currentAppearance"> data </p>

It's on a different line for each profile so I can't just load line 200 and hope for the best.

Does anyone have any solution (preferably using php) that searches for that tag on an external page and then loads the specific tag into a div?

I hope I've been clear I am quite new to all this back end stuff!

  • 写回答

2条回答 默认 最新

  • douyin2883 2012-07-20 12:07
    关注

    First I would use to grab the content from the webpage: http://www.php.net/manual/en/curl.examples-basic.php

    $url = 'http://www.some-domain.com/some-page';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $htmlContent = curl_exec($curl);
    curl_close($curl);
    

    Then using DomDocument (http://ca3.php.net/manual/en/book.dom.php) you'll be able to access the right div based on its ID for instance.

    $doc = new DOMDocument();
    $doc->loadHTML($htmlContent);
    foreach ($pElements as $pEl) {
      if ($pEl->getAttribute('class') == 'currentAppearance') {
        $pContent =  $pEl->nodeValue;
      }
    }
    

    $pContent is now set with the content of the paragraph with class currentAppearance

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办