1

我正在使用 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 ”或其他内容,它会返回链接。

4

2 回答 2

1

这是应该怎么做的

$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); 
于 2012-01-05T07:26:24.910 回答
0

您不应该使用 str_get_html 而不是 file_get_html 吗?因为 $ouput 是一个字符串!

于 2011-07-01T23:57:05.147 回答