php语言 百分网手机站

PHP可以灵活配置使用的采集器(3)

时间:2020-08-06 10:44:01 php语言 我要投稿

PHP可以灵活配置使用的采集器

  $$field = WEB_HOST.trim($$field);

  else

  $$field = WEB_HOST.'/'.trim($$field);

  $sth->bindValue(':'.$field, trim($$field));

  }

  if (INSERT_DB)

  $sth->execute();

  $sth->closeCursor();

  $this->write( '休息,暂停'.SLEEP_TIME.'秒后继续抓取...');

  sleep(SLEEP_TIME);

  }

  }

  else

  {

  $this->write('列表页面没有抓取到内容,所以过滤掉');

  }

  }

  $this->write('', true);

  }

  protected function closetags($html)

  {

  // 不需要补全的标签

  $arr_single_tags = array('meta', 'img', 'br', 'link', 'area');

  // 匹配开始标签

  preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);

  $openedtags = $result[1];

  // 匹配关闭标签

  preg_match_all('#</([a-z]+)>#iU', $html, $result);

  $closedtags = $result[1];

  // 计算关闭开启标签数量,如果相同就返回html数据

  $len_opened = count($openedtags);

  if (count($closedtags) == $len_opened) {

  return $html;

  }

  // 把排序数组,将最后一个开启的标签放在最前面

  $openedtags = array_reverse($openedtags);

  // 遍历开启标签数组

  for ($i = 0; $i < $len_opened; $i++) {

  // 如果需要补全的标签

  if (!in_array($openedtags[$i], $arr_single_tags)) {

  // 如果这个标签不在关闭的标签中

  if (!in_array($openedtags[$i], $closedtags)) {

  // 直接补全闭合标签

  $html .= '</' . $openedtags[$i] . '>';

  } else {

  unset($closedtags[array_search($openedtags[$i], $closedtags)]);

  }

  }

  }

  return $html;

  }

  protected function init_check()

  {

  if (!$this->check_curl_support())

  $this->write('对不起,请先开启CURL的类库的支持,否则无法执行', true);

  $this->check_mysql_connect();

  $this->write('程序初始化检查通过,执行后续的流程...');

  }

  private function get($url, $data = array())

  {

  $this->write('开始执行抓取: '.$url);

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  //curl_setopt($ch, CURLOPT_USERAGENT, "Baiduspider+(+http://www.baidu.com/search/spider.htm)");

  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

  curl_setopt($ch, CURLOPT_HEADER, 0);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_HTTPHEADER, $data);

  $ret = curl_exec($ch);

  $error = curl_error($ch);

  curl_close($ch);

  unset($ch);

  if (!empty($error))

  {

  $this->write('程序抓取URL: '.$url.'发生错误,错误信息: '.$error);

  return false;

  }

  if (WEB_CHARSET != 'utf-8')

  $ret = iconv(WEB_CHARSET, 'utf-8', $ret);

  return $ret;

  }

  //when check finish,mysql connect will auto close

  private function check_mysql_connect()

【PHP可以灵活配置使用的采集器】相关文章:

php学习之php配置09-09

如何配置php环境09-06

PHP安装与配置09-02

PHP配置文件详解php.ini09-03

如何正确配置 Nginx + PHP09-02

PHP语言的使用特性09-15

vps主机如何配置php环境09-30

如何使用PHP框架09-15

Session在PHP中的使用09-16

使用php伪造referer的方法09-10