我正在使用CURL登录Craigslist来抓取我发布的列表的状态。我遇到的问题是将HTML从CURL $output传输到file_get_html。虽然Craigslist的状态实际上是嵌套在TR元素中的,但我只是想测试一下最基本的函数,看看是否有东西通过(即链接抓取)。他们不是。
例如,这不起作用:
$cookie_file_path = getcwd()."/cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.craigslist.org/login?LoginType=L&step=confirmation&originalURI=%2Flogin&rt=&rp=&inputEmailHandle='.$email.'&inputPassword='.$password.'&submit=Log%20In');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.craigslist.org');
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;
//
include_once('simple_html_dom.php');
$html = file_get_html($output);
//find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>'; 我知道这个表达式是有效的,因为如果我输入'http://google.com‘或者其他什么东西,它就会返回链接。
发布于 2012-01-05 15:26:25
这就是应该怎么做的
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.sitename.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$str = curl_exec($curl);
curl_close($curl);
$html= str_get_html($str); 发布于 2011-07-02 07:57:05
你不应该使用str_get_html而不是file_get_html吗?因为$ouput是一个字符串!
https://stackoverflow.com/questions/3437578
复制相似问题