A. php抓取網頁指定內容
<?php
/*
* 如下: 方法有點笨
* 抓取網頁內容用 PHP 的正則
* 用JS每隔5分鍾刷新當前頁面---即重新獲取網頁內容
*
* 註: $mode中--<title></title>-更改為所需內容(如 $mode = "#<a(.*)</a>#";>獲取所有鏈接)
*
* window.location.href="http://localhost//refesh.php";中的http://localhost//refesh.php
* 更改為自己的URL----作用:即刷新當前頁面
*
* setInterval("ref()",300000);是每隔300000毫秒(即 5 * 60 *1000 毫秒即5分鍾)執行一次函數 ref()
*
* print_r($arr);輸出獲得的所有內容 $arr是一個數組 可根據所需輸出一部分(如 echo $arr[1][0];)
* 若要獲得所有內容 可去掉
* $mode = "#<title>(.*)</title>#";
if(preg_match_all($mode,$content,$arr)){
print_r($arr);
echo "<br/>";
echo $arr[1][0];
}
再加上 echo $content;
*/
$url = "http://www..com"; //目標站
$fp = @fopen($url, "r") or die("超時");
$content=file_get_contents($url);
$mode = "#<title>(.*)</title>#";
if(preg_match_all($mode,$content,$arr)){
//print_r($arr);
echo "<br/>";
echo $arr[1][0];
}
?>
<script language="javaScript" type="text/javascript">
<--
function ref(){
window.location.href="http://localhost//refesh.php";
}
setInterval("ref()",300000);
//-->
</script>
B. 有沒有辦法實現PHP代理抓取網頁內容
可以呀。
用snoopy的類,網上有snoopy.class.php,你自行網路查找。
snoopy的類可以設置$proxy_host參數,設置代理主機,$proxy_port是代理主機埠。你下載一個下來,網上的教程很多,看看應該明白。
至於調用proxy.txt,輪換ip的問題,我覺得可用代理不是很多的話,可以設置成隨機選擇代理就好了。你採集的那個網站記錄的是你代理伺服器的ip
C. php正則提取HTML中的內容
那就無需正則了!
php本身就有一個函數:strip_tags()
這個函數有2個參數
第一個:需要過濾的字元串, 在這里也就是你說的html, 這個函數必須
第二個:要保留的html標簽, 就是設置你不想過濾掉的html標簽, 這個函數可選!
在第二個參數預設的情況下, 會將所有html標簽過濾掉!
還要什麼正則呢???
D. 用php獲取指定網頁內容
functiongetRemoteRes($url,$postfields=NULL,$timeout=60){
$ci=curl_init();
curl_setopt($ci,CURLOPT_URL,$url);
curl_setopt($ci,CURLOPT_HEADER,FALSE);
curl_setopt($ci,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ci,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ci,CURLOPT_TIMEOUT,$timeout);
curl_setopt($ci,CURLOPT_POST,TRUE);
if(is_array($postfields)){
$field_str="";
foreach($postfieldsas$k=>$v){
$field_str.="&$k=".urlencode($v);
}
curl_setopt($ci,CURLOPT_POSTFIELDS,$field_str);
}
$response=curl_exec($ci);
if(curl_errno($ci)){
return'ERRNO!';
}else{
$httpStatusCode=curl_getinfo($ci,CURLINFO_HTTP_CODE);
if(200!==$httpStatusCode){
return'ERRNO!';
}
}
curl_close($ci);
return$response;
}
先用以上函數獲取指定的網頁,然後從返回的數據中解析出你要的數據.可以使用正則表達式來提取,這要根據你要獲取的頁面源代碼來判斷了.暫時未知,以上只是提供一個思路給你.
E. 用PHP正則表達式提取頁面內容
<?php
$theurl="http://www.kitco.cn/cn/";
if (!($contents = file_get_contents($theurl)))
{
echo 'Could not open URL';
exit;
}
/*
$contents=preg_replace('/<.+?>/', '', $contents);
*/
if (preg_match("/<td class=\"tableHeader\" align=\"left\">原油價格([^^]*?)<\/tr>/u",$contents,$matches))
{
print "A match was found:".strip_tags($matches[0]);
} else {
print "A match was not found.<br />";
}
?>
試試這樣
------------------------------------
呵呵,上邊這段已經把你那行注釋掉了,先找到唯一的一段代碼,取出來你想要的以後以後,再去掉標簽,你運行一下試試
運行結果:
A match was found:原油價格 68.11 +0.95
應該是你想要的結果吧?
F. 用php提取網頁中內容請教
$str='<script id="js_load" language="javascript" src="/match_data_xml/loaddm.php?md=2010-01-01&1374176264778"></script>';
$a=preg_match("/src=[\\\'| \\\"]([^(\\\'|\\\")])+[\\\'|\\\"]>/is",$str,$arr);
$new_str=substr($arr[0], strpos($arr[0],'=')+1);
echo $str_need=substr(trim($new_str), 1,strlen(trim($new_str))-3);
$str_need 入庫
G. PHP如何正則表達式提取網頁內容
如果你要<div class="nav" monkey="nav">和<div class="head-ad">之間的所有源碼,用 preg_match 就可以,不用preg_match_all ,如果你要裡面的所有的 <li></li>標簽中的內容,可以用preg_match_all
//提取所有代碼
$pattern = '/<div class="nav" monkey="nav">(.+?)<div class="head-ad">/is';
preg_match($pattern, $string, $match);
//$match[0] 即為<div class="nav" monkey="nav">和<div class="head-ad">之間的所有源碼
echo $match[0];
//然後再提取<li></li>之間的內容
$pattern = '/<li.*?>(.+?)<\/li>/is';
preg_match_all($pattern, $match[0], $results);
$new_arr=array_unique($results[0]);
foreach($new_arr as $kkk){
echo $kkk;
}
H. PHP如何根據URL抓取不同網站的文章內容
我自己有寫一套採集系統,PHP 和 python 都有,思想就是你要採集的網站每個網站寫一個文件,裡面存一些變數,比如你要採集的內容在 a網站是在 DIV裡面,在b網站是在p裡面,這樣你就要建立兩個文件,分別用來存這些無法預知的變數,然後通過公用的下載程序,把這個變數替換進去。如果你有更好的方法,希望能多交流。
I. PHP怎麼修改網頁文字並提取圖片啊
<?php
$text1="第一句";
$text2="第二句";
$text3="第三句";
$url='http://tp.388g.com/aosbegin00006.php?id=736&text1={text1}&text2={text2}&text3={text3}&text4=undefined&text5=undefined&rnd=';
$url=str_replace('{text1}',urlencode($text1),$url);
$url=str_replace('{text2}',urlencode($text2),$url);
$url=str_replace('{text3}',urlencode($text3),$url);
$url=$url.time();
$content=file_get_contents($url);
preg_match('/<outputimg>.(.*?)</outputimg>/is',$content,$matched);
$imgUrl='http://tp.388g.com'.$matched[1];
echo$imgUrl;
exit;
J. php提取網頁中變數文字
可以用file_get_contents,也可以用curl,但是首先得,你要提取的文字有標志圍著,比如<span class="mark">某某某某</span>,然後根據class="mark"就可以截取了,用substr等都可以。