以前我用過(guò)querylist插件抓數(shù)據(jù),服務(wù)器寫(xiě)和定時(shí)器,每天固定時(shí)間去運(yùn)行腳本。朝這個(gè)方式試試
目前成都創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、蘇尼特左網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
高并發(fā)下數(shù)據(jù)的更新,應(yīng)該 update table xxx set num = num - 1 的方式,這種方式可以保證數(shù)據(jù)的正確性。
但是會(huì)出現(xiàn) num 為負(fù)數(shù)的問(wèn)題,如果庫(kù)存為負(fù)數(shù),顯然是不合理的。
于是,需要將 num 字段設(shè)置為 無(wú)符號(hào)整型,這樣就不會(huì)出現(xiàn)負(fù)數(shù)了,因?yàn)椋绻麥p到負(fù)數(shù),就會(huì)更新失敗。
但是這種依然會(huì)造成很多無(wú)用的更新語(yǔ)句的執(zhí)行,是不合理的。
于是,update table xxx set num = num - 1 where num 0,
這樣當(dāng) num 等于0之后就不會(huì)去更新數(shù)據(jù)庫(kù)了,減少了很多無(wú)用的開(kāi)銷。
這種方式被稱作“樂(lè)觀鎖”
此外,對(duì)于搶紅包這種非整數(shù)的操作,我們應(yīng)該轉(zhuǎn)換為整數(shù)的操作。
關(guān)于搶購(gòu)超賣的控制
一般搶購(gòu)功能是一個(gè)相對(duì)于正常售賣系統(tǒng)來(lái)說(shuō)獨(dú)立的子系統(tǒng),這樣既可以防止搶購(gòu)時(shí)的高并發(fā)影響到正常系統(tǒng),
也可以做到針對(duì)于搶購(gòu)業(yè)務(wù)的特殊處理。
在后臺(tái)設(shè)計(jì)一些功能,可以就昂正常的商品加入到搶購(gòu)活動(dòng)中并編輯成為搶購(gòu)商品,寫(xiě)入到搶購(gòu)商品表,當(dāng)然
也可以把搶購(gòu)商品表寫(xiě)入redis而不是數(shù)據(jù)表。并且在原商品表寫(xiě)入一個(gè)同樣的商品(id相同,用于訂單查看,
此商品不可購(gòu)買)
如果是數(shù)據(jù)表,為了控制超賣,需要對(duì)表進(jìn)行行鎖,更新的時(shí)候帶上 where goods_amount 0。
如果是redis,使用 hincrby 一個(gè)負(fù)數(shù)來(lái)減庫(kù)存,并且 hincrby 會(huì)返回改變后的值,再來(lái)判斷返回值是否大于0,
因?yàn)閞edis每個(gè)命令都是原子性的,這樣不用鎖表就可控制超賣。
一、用file_get_contents以get方式獲取內(nèi)容,需要輸入內(nèi)容為:
1、?php
2、$url='';
3、$html=file_get_contents($url);
4、echo$html;
5、?
二、用file_get_contents函數(shù),以post方式獲取url,需要輸入內(nèi)容為
1、?php
2、$url='';
3、$data=array('foo'='bar');
4、$data=http_build_query($data);
5、$opts=array(
6、'http'=array(
7、?'method'='POST',
8、?'header'="Content-type:application/x-www-form-urlencoded\r\n".
9、??????????"Content-Length:".strlen($data)."\r\n",
10、?'content'=$data
11、)
12、);
13、$ctx=stream_context_create($opts);
14、$html=@file_get_contents($url,'',$ctx);
15、?
三、用fopen打開(kāi)url,以get方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$fp=fopen($url,'r');
3、$header=stream_get_meta_data($fp);//獲取信息
4、while(!feof($fp)){
5、$result.=fgets($fp,1024);
6、}
7、echo"urlheader:{$header}br":
8、echo"urlbody:$result";
9、fclose($fp);
10、?
四、用fopen打開(kāi)url,以post方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$data=array('foo2'='bar2','foo3'='bar3');
3、$data=http_build_query($data);
4、$opts=array(
5、'http'=array(
6、'method'='POST',
7、'header'="Content-type:application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n".
8、"Content-Length:".strlen($data)."\r\n",
9、'content'=$data
10、)
11、);
12、$context=stream_context_create($opts);
13、$html=fopen(';id2=i4','rb',false,$context);
14、$w=fread($html,1024);
15、echo$w;
16、?
五、用fsockopen函數(shù)打開(kāi)url,以get方式獲取完整的數(shù)據(jù),包括header和body,需要輸入內(nèi)容為
1、?php
2、functionget_url($url,$cookie=false)
3、{
4、$url=parse_url($url);
5、$query=$url[path]."?".$url[query];
6、echo"Query:".$query;
7、$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
8、if(!$fp){
9、returnfalse;
10、}else{
11、$request="GET$queryHTTP/1.1\r\n";
12、$request.="Host:$url[host]\r\n";
13、$request.="Connection:Close\r\n";
14、if($cookie)$request.="Cookie:??$cookie\n";
15、$request.="\r\n";
16、fwrite($fp,$request);
17、while(!@feof($fp)){
18、$result.=@fgets($fp,1024);
19、}
20、fclose($fp);
21、return$result;
22、}
23、}
24、//獲取url的html部分,去掉header
25、functionGetUrlHTML($url,$cookie=false)
26、{
27、$rowdata=get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body=stristr($rowdata,"\r\n\r\n");
31、$body=substr($body,4,strlen($body));
32、return$body;
33、}
34、?returnfalse;
35、}
36、?
參考資料:
php-file_get_contents
有很多方法的呀,
1)字符串截取,$result
=
substr($whole,
0,
4);
2)用空格分割字符串到數(shù)組中:$ary
=
explode('
',
$whole);
$result
=
$ary[0]
可以用以下4個(gè)方法來(lái)抓取網(wǎng)站 的數(shù)據(jù):
1. 用 file_get_contents 以 get 方式獲取內(nèi)容:
?
$url = '';
$html = file_get_contents($url);
echo $html;
2. 用fopen打開(kāi)url,以get方式獲取內(nèi)容
?
$url = '';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函數(shù),以post方式獲取url
?
$data = array(
'foo'='bar',
'baz'='boom',
'site'='',
'name'='nowa magic');
$data = http_build_query($data);
//$postdata = http_build_query($data);
$options = array(
'http' = array(
'method' = 'POST',
'header' = 'Content-type:application/x-www-form-urlencoded',
'content' = $data
//'timeout' = 60 * 60 // 超時(shí)時(shí)間(單位:s)
)
);
$url = "";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl庫(kù),使用curl庫(kù)之前,可能需要查看一下php.ini是否已經(jīng)打開(kāi)了curl擴(kuò)展
$url = '';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
分享標(biāo)題:抓取數(shù)據(jù)php 抓取數(shù)據(jù) 英文
文章源于:http://sd-ha.com/article6/doijeig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、云服務(wù)器、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)、搜索引擎優(yōu)化、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)