【问题标题】:Extract website html data using php使用php提取网站html数据
【发布时间】:2015-06-09 13:59:38
【问题描述】:

我使用此代码提取网站https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4000917512

但我有一个错误Fatal error: Call to a member function find() on a non-object in index.php 以及我想在像<span id="SpanPhoneNumber" dir="ltr">02-26981106</span><span id="SpanCurrentBalance">19.30</span>这样的跨度元素之间提取它

include_once("simple_html_dom.php");
//use curl to get html content
function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       return @curl_exec($ch);
}
$html=getHTML("https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4000917512",10);
// Find all images on webpage
foreach($html->find("img") as $element)
echo $element->src . '<br>';

// Find all links on webpage
foreach($html->find("a") as $element)
echo $element->href . '<br>';

【问题讨论】:

    标签: php curl


    【解决方案1】:

    你忘了用 str_get_html() 来自 sipmle_dom_html 库的 $html 变量的函数:

    $response=getHTML("https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4000917512",10);
    $html = str_get_html($response);
    

    使用 https 页面时,您可能还需要在函数内部使用:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    

    【讨论】:

    • 谢谢我像你说的那样说但是同样的错误Call to a member function find() on a non-object in
    • 你认为问题出在网站上吗?因为我现在尝试另一个链接并且工作良好。
    • 试试 $this->html->find
    • @anantkumarsingh 我试试看,告诉我这个错误Fatal error: Using $this when not in object context
    • 使用 DomDocument。供参考检查链接:-stackoverflow.com/questions/11567632/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多