这里我的代码运行正常
include ("classes/phpQuery-onefile.php");
$url = "http://www.website.com";
$rawHTML = fetchRawData ( $url );
$parsedData = parseSpecificData ( $rawHTML );
//displayData ( $parsedData );
print '<pre>';
print_r($parsedData);
print '</pre>';
//our fetching function
function fetchRawData($url) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); //return the output as a variable
curl_setopt ( $ch, CURLOPT_FAILONERROR, true ); //fail if error encountered
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true ); //allow redirects
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 ); //time out length
$data = curl_exec ( $ch );
if (! $data) {
echo "<br />cURL error:<br/>\n";
echo "#" . curl_errno ( $ch ) . "<br/>\n";
echo curl_error ( $ch ) . "<br/>\n";
echo "Detailed information:";
var_dump ( curl_getinfo ( $ch ) );
die ();
}
curl_close ( $ch );
return $data;
}
//our parsing function
function parseSpecificData($data) {
$parsedData = array ();
phpQuery::newDocumentHTML ( $data );
foreach ( pq ( ".box a" ) as $link ) {
//print '<pre>';print_r($link);print'</pre>';
$title = pq ( $link )->text ();
if ($title) {
$parsedData [] = array ("title" => $title,
"href" => pq ( $link )->attr ( 'href' )
);
}
}
return $parsedData;
}
这里有必须解析的页面内容
<div class="box">
<span class='name'>Name</span><br>
<span class="address">Address</span><br>
<a href='http://www.website.com'>www.website.com</a>
</div>
这里有输出
Array
(
[0] => Array
(
[title] => www.website.com
[href] => http://www.website.com
)
)
我的问题是如何使用键name
和address
创建数组。
在问之前我真的一直在搜索和尝试,但有一些我在phpQuery类中没有理解的东西。
谢谢你的帮助。
亚历