php 网页采集入库程序代码

 更新时间:2016年11月25日 15:35  点击:1942
网页采集现在用到最多是工具了,像最受站长欢迎的就是火车头了,但有一些站长喜欢使用网页来自定义采集了,下面一起来看一个php 网页采集入库程序代码

php 网页采集程序总结,最近帮朋友做了个采集程序

以www.xxxx.com/shop_list.php?page=1&province=%B1%B1%BE%A9为例

%B1%B1%BE%A9是gb2312的转码,例如

$aa=”北京”;
$aa = @iconv(“utf-8″, “gb2312″,$aa);
echo $bb=urlencode($aa);

我们通过file_get_contents($url) 抓取网页 当然也可以是curl

function getHtml($url){
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch2);
curl_close($ch2);
return $html;
}

抓取我们想要的页面数据,可以设定从哪个位置到哪个位置的区间,取出中间数据,通过以下方法实现

function findneed($wholestr,$strkey1,$strkey2)
{
$num1 = strpos($wholestr , $strkey1)+strlen($strkey1);
$num2 = strpos($wholestr ,$strkey2);
$needstr =substr($wholestr ,$num1,$num2-$num1 );
return $needstr;
}
当然这是一种方法,我们只要写出一个php即可,根据分页抓取,但是如果都放在循环里面,岂不是很慢

我们介绍另个算法

<script>
location.href=”index.php?page=<?=$page?>&provinceIndex=<?=$provinceIndex?>&totalPage=<?=$totalPage?>”;
</script>

通过实现网页跳转页数,抓取,访问程序,不断跳转页数,把当前url的page 数组保存到数据库

其他的无非是些正则表达式的用法:

比如我们想取页面中的所有城市

province

可用preg_match_all(‘/<select name=”province”(.*?)>(.*?)<\/select>/s’,$html,$selects);即可

(.*?)表示任意字符   . 是任何东西 * 是0至无限  ? 是0至1

还有种算法是递归,类似循环取值

function collectionProvinceData($url,$province,$page=1,$totalPage=-1){
if($page>$totalPage&&$totalPage>-1){
return false;
}
$collectionUrl = $url."?page=".$page."&province=".urlencode(iconv('UTF-8', 'GB2312', $province));
echo "当前url:".$province."第{$page}页 url".$collectionUrl."<hr/>";
$html = getHtml($collectionUrl);
$html = mb_convert_encoding($html, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
if($totalPage==-1){
$latestPageNum = getLatestPageNum($html);
if($latestPageNum>0){
$totalPage = $latestPageNum;
}
}
$dataRows = getDataRows($html);
saveDataRowsOrNot($dataRows);
if(!empty($dataRows)){
$page++;
}
ob_flush();
flush();
collectionProvinceData($url,$province,$page,$totalPage);
}

php的慢速日志引起的Mysql 2013错误怎么办,下面我们就一起来看看这个问题的解决办法,希望例子能够帮助到各位。


Description:
————
If mysql query is longer as request_slowlog_timeout, connection breaks.

Test script:


<?php
// request_slowlog_timeout = 10s  (at /etc/php5/fpm/php-fpm.conf)
 
// $mysqli =
// ...
$query = "SELECT SLEEP (15)";
 
$res = $mysqli->query($query);
if (!$res) {
    echo $mysqli->error; // Error Code: 2013. Lost connection to MySQL server during query
    exit;
}
Expected result:
—————-
connection must be preserved and the request should be executed

缩略图可以通过gd库来实现,下面我们一起来看一个简单的php使用GD库实现文字图片水印及缩略图例子,希望此例子能够为大家带来有效的帮助。


我们要使用gd库就必须先打开gd库,具体如下

Windows下开启PHP的GD库支持

找到php.ini,打开内容,找到:

;extension=php_gd2.dll

把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了。


具体可以参考下文:http://www.111cn.net/phper/php/48352.htm


一:添加文字水印 使用方法

require 'image.class.php'
$src="001.jpg";
$content="hello";
$font_url="my.ttf";
$size=20;
$image=new Image($src);
$color=array(
0=>255,
1=>255,
2=>255,
2=>20
);
$local=array(
'x'=>20,
'y'=>30
);
$angle=10;
$image->fontMark($content,$font_url,$size,$color,$local,$angle);
$image->show();

二:图片缩略图 使用方法:

require 'image.class.php'
$src="001.jpg";
$image=new Image($src);
$image->thumb(300,200);
$image->show();

三:image.class.php

class image{
private $info;
private $image;
public function __contruct($src){
$info= getimagesize($src);
$this->info=array(
'width'=> $info[0],
'height'=>$info[1],
'type'=>image_type_to_extension($info[2],false),
'mime'=>$info['mime'],
 
);
$fun="imagecreatefrom{$this->info['type']}";
 
$this->image= $fun($src);
 
}
 
//缩略图
public function thumd($width,$height){
$image_thumb= imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
imagedestroy($this->image);
$this->image=$image_thumb;
 
}
//文字水印
public function fontMark($content,$font_url,$size,$color,$local,$angle){
 
$col=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
$text=imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
}
//输出图片
public function show()
{
header("Content-type:",$this->info['mime']);
 
$func="image{$this->info['type']}";
$func($this->image);
 
}
public function save($nwename){
$func="image{$this->info['type']}";
//从内存中取出图片显示
$func($this->image);
//保存图片
$func($this->image,$nwename.$this->info['type']);
 
}
public function _destruct(){
 
imagedestroy($this->image);
}
 
}

微信开发已经是现在程序员必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单,这里我们一起来看一个微信自动登陆注册的例子。


php 微信扫码 pc端自动登陆注册 用的接口scope 是snsapi_userinfo,微信登陆一个是网页授权登陆,另一个是微信联合登陆

网页授权登陆:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

微信联合登陆:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN

一:首先把微信链接带个标识生成二维码

比如链接为 https://open.weixin.qq.com/connect/oauth2/authorize?appid=’.$appid.’&redirect_uri=’.$url.’&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect’  我们可以在state上做文章,因为state你传入什么微信那边返回什么

可以作为服务器与微信段的一个标识

public function creatqrAction(){
 
if($_GET['app']){
$wtoken=$_COOKIE['wtoken'];
$postdata=$_SESSION['w_state'];
if($wtoken){
$postdata=$wtoken;
}
include CONFIG_PATH . 'phpqrcode/'.'phpqrcode.php'
$sh=$this->shar1();
$value="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&redirect_uri=http://www.xxx.net/login/wcallback&response_type=code&scope=snsapi_userinfo&state=".$postdata."&connect_redirect=1#wechat_redirect";
 
$errorCorrectionLevel = "L";
 
$matrixPointSize = "5";
 
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
}
 
}

此时生成了二维码 state是标识,phpqrcode可以在文章末尾下载,这样我们设置了回调地址http://www.xxx.net/login/wcallback

就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了

state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');
 
}, false);

也可以授权登陆成功后跳转到微信服务号关注页面

header("Location: weixin://profile/gh_a5e1959f9a4e");

wcallback方法做处理登陆

$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php'
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
 
if (empty($code)) $this->showMessage('授权失败');
try{
 
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
 
$token = json_decode($this->https_request($token_url));
 
}catch(Exception $e)
{
print_r($e);
}
 
if (isset($token->errcode)) {
echo '

错误:

'.$token->errcode;
echo '

错误信息:

'.$token->errmsg;
exit;
}
 
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '

错误:

'.$access_token->errcode;
echo '

错误信息:

'.$access_token->errmsg;
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '

错误:

'.$user_info->errcode;
echo '

错误信息:

'.$user_info->errmsg;
exit;
}
//打印用户信息
// echo '

'
// print_r($user_info);
// echo '

'

phpqrcode类库下载在此不提供各位可以百度搜索下载

magento微信扫码网站自动登录的例子

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN

查看授权后接口调用(UnionID),不难发现填写回调地址,用户确认登陆pc端即可跳转

获取UnionID方法


public function wcallbackAction(){
 
$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php';
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
 
if (empty($code)) $this->showMessage('授权失败');
try{
 
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
 
$token = json_decode($this->https_request($token_url));
 
}catch(Exception $e)
{
print_r($e);
}
 
if (isset($token->errcode)) {
echo '<h1>错误:</h1>'.$token->errcode;
echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
exit;
}
 
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '<h1>错误:</h1>'.$access_token->errcode;
echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '<h1>错误:</h1>'.$user_info->errcode;
echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
exit;
}
//打印用户信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';
 
//获取unionid
 
$uid=$user_info->unionid;
 
}
 
//用户操作处理 分为再次登录和第一次登陆
 
$sql="select h_user_id from dtb_user_binded as t1 left join dtb_user_weixin as t2 on t1.u_id=t2.id where t1.u_type='".
User::$arrUtype['weixin_num_t']."' and t2.openid='$user_info->unionid'";
$h_user_id = Core_Db::getOne($sql);
if(!empty($h_user_id)){//该weixin号再次登录
 
}{//该weixin号第一次登录
 
}

操作iframe对于许多的前段来讲觉得是一个不可靠的方式了,其实操作iframe是非常的不错并且兼容性也好,下面我们来看一个经典的关于操作iframe的例子。

虽然我们在web开发中使用iframe比较少,但是iframe还是有它值得使用的场景,比如嵌入外部广告等。在使用iframe过程中我们可能会遇到很多问题,例如iframe父子窗口传值,iframe自适应高度,iframe跨域通信等等。


实际应用中,我们常常要在父窗口中直接获取子窗口页面的元素,并对其进行操作。今天我来使用实例给大家介绍如何使用Javascript来进行iframe父子窗口之间相互操作dom对象,开始动手。
HTML

为了更好的讲解和演示,我们准备3个页面,父级页面index.html的html结构如下。另外两个子页面分别为iframeA.html和iframeB.html。我们需要演示通过父页面获取和设置子页面iframeA.html中元素的值,以及演示通过子页面iframeB.html设置子页面iframeA.html相关元素的值以及操作父页面index.html元素。
 
<div class="opt_btn">
    <button onclick="getValiframeA();">父窗口获取iframeA中的值</button>
    <button onclick="setValiframeA();">父窗口设置iframeA中的值</button>
    <button onclick="setBgiframeA();">父窗口设置iframeA的h1标签背景色</button>
</div>

<div id="result">--操作结果--</div>
 
<div class="frames">
    <iframe id="wIframeA" name="myiframeA" src="iframeA.html" scrolling="no" frameborder="1"></iframe> 
    <iframe id="wIframeB" name="myiframeB" src="iframeB.html" scrolling="no" frameborder="1"></iframe>
</div>
iframeA.html布置一个h1标题,以及一个输入框和一个p段落,结构如下:
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>helloweba.com</title>
</head>
 
<body>
<h1 id="title">iframe A</h1> 
<input type="text" id="iframeA_ipt" name="iframeA_ipt" value="123">
<p id="hello">helloweba.com欢迎您!</p> 
</body>
</html>

iframeB.html同样布置h1标题和段落以及输入框。iframe有两个按钮,调用了javascript,相关代码等下在js部分会描述。
 
<html>
<head>
<meta charset="utf-8">
<title>helloweba.com</title>
</head>
 
<body>
<h1>iframe B</h1> 
<p id="hello">Helloweb.com</p> 
<input type="text" id="iframeB_ipt" name="iframeB_ipt" value="1,2,3,4">
<button onclick="child4parent();">iframeB子窗口操作父窗口</button> 
<button onclick="child4child();">iframeB操作子窗口iframeA</button> 
 
</body>
</html>

Javascript

页面html都布置好了,现在我们来看Javascript部分。
首先我们来看index.html父级页面的操作。JS操作iframe里的dom可是使用contentWindow属性,contentWindow属性是指指定的frame或者iframe所在的window对象,在IE中iframe或者frame的contentWindow属性可以省略,但在Firefox中如果要对iframe对象进行编辑则,必须指定contentWindow属性,contentWindow属性支持所有主流浏览器。
我们自定义了函数getIFrameDOM(),传入参数iID即可获取iframe,之后就跟在当前页面获取元素的操作一样了。
 
function getIFrameDOM(iID){
    return document.getElementById(iID).contentWindow.document;
}
index.html的三个按钮操作代码如下:
 
function getValiframeA(){
    var valA = getIFrameDOM("wIframeA").getElementById('iframeA_ipt').value;
    document.getElementById("result").innerHTML = "获取了子窗口iframeA中输入框里的值:<span style='color:#f30'>"+valA+"</span>";
}
 
function setValiframeA(){
    getIFrameDOM("wIframeA").getElementById('iframeA_ipt').value = 'Helloweba';
    document.getElementById("result").innerHTML = "设置了了子窗口iframeA中输入框里的值:<span style='color:#f30'>Helloweba</span>";
}
 
function setBgiframeA(){
    getIFrameDOM("wIframeA").getElementById('title').style.background = "#ffc";
}

保存后,打开index.html看下效果,是不是操作很简单了。
好,iframeB.html的两个按钮操作调用了js,代码如下:
 
function child4child(){
    var parentDOM=parent.getIFrameDOM("wIframeA");
    parentDOM.getElementById('hello').innerHTML="<span style='color:blue;font-size:18px;background:yellow;'>看到输入框里的值变化了吗?</span>";
    parentDOM.getElementById('iframeA_ipt').value = document.getElementById("iframeB_ipt").value;
    parent.document.getElementById("result").innerHTML="子窗口iframeB操作了子窗口iframeA";
}
function child4parent(){
    var iframeB_ipt = document.getElementById("iframeB_ipt").value;
    parent.document.getElementById("result").innerHTML="<p style='background:#000;color:#fff;font-size:15px;'>子窗口传来输入框值:<span style='color:#f30'>"+iframeB_ipt+"</span></p>";
}
子页面iframeB.html可以通过使用parent.getIFrameDOM()调用了负页面的自定义函数getIFrameDOM(),从而就可以对平级的子页面iframeA.html进行操作,子页面还可以通过使用parent.document操作父级页面元素。
本文结合实例讲解了Javascript对iframe的基本操作,接下来我们会介绍iframe的应用:自适应高度以及iframe跨域问题,敬请关注。

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • php根据用户语言跳转相应网页

    当来访者浏览器语言是中文就进入中文版面,国外的用户默认浏览器不是中文的就跳转英文页面。 <&#63;php $lan = substr(&#8194;$HTTP_ACCEPT_LANGUAGE,0,5); if ($lan == "zh-cn") print("<meta http-equiv='refresh' c...2015-11-08
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • 腾讯视频怎么放到自己的网页上?

    腾讯视频怎么放到自己的网页上?这个问题是一个基本的问题,要把腾讯视频放到自己的网页有许多的办法,当然一般情况就是直接使用它们的网页代码了,如果你要下载资源再放到...2016-09-20
  • vue项目,代码提交至码云,iconfont的用法说明

    这篇文章主要介绍了vue项目,代码提交至码云,iconfont的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-30