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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。