用PHP爬取51job上苏州地区的PHP职位信息
简单的PHP小爬虫。
效果可以看如下链接:
点击打开链接。
本Demo用到了'simple_html_demo.php'这个Dom操作类库。
<?php
header('Content-Type: text/html; charset=UTF-8');
include 'simple_html_dom.php';
$time= date('Y-m-d H:i:s',time());
echo '<h1 style="margin: 5PX;">用PHP爬取51job上苏州地区的PHP职位信息</h1>
爬取时间:<b>'.$time.'</b>。<br>';
echo '<br><br><br><br>';
$stime=microtime(true); //开始计时
$html = file_get_html('http://search.51job.com/list/070300,000000,0000,00,9,99,PHP,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
$pages=$html->find("div[class=p_wp] div[class=p_in] span[class=td]");
$page=preg_replace('/[^0-9]/','',$pages[0]); //获取页数
$number=0;
for ($x=1; $x<=$page; $x++) {
$html = file_get_html('http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=070300%2C00&district=000000&funtype=0000&industrytype=00&issuedate=9&providesalary=99&keyword=PHP&keywordtype=2&curr_page='.$x.'&lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&list_type=0&fromType=14&dibiaoid=0&confirmdate=9');
$pageinfo=$html->find("div[id=resultList] div[class=el]");
array_shift($pageinfo);//去除第一条不对的信息
foreach ($pageinfo as $key) {
$company=$key->find("span[class=t2] a");
$address=$key->find("span[class=t3]");
$money=$key->find("span[class=t4]");
echo $company[0];
//echo $job[0];
echo ' 地址:'.$address[0];
echo ' 薪资:'.$money[0];
echo '<br>';
$number++;
};
}
$etime=microtime(true);//获取程序执行结束的时间
$total=$etime-$stime; //计算差值
echo '<span style="position:absolute;top:70px">用时:<b>'.$total.'</b>秒。<br>苏州一共有<b>'.$number.'</b>条PHP职位需求信息。</span>';
$html->clear();
?>