2012-09-20 50 views
0

編程php抓取器。我想抓取一些文本,然後將抓取的內容與上次掃描時存儲在我的數據庫中的內容進行比較。一切正常。但我想掃描網址在哪裏是一個「過濾器」的形式,所以它張貼一些數據,因此我不能看到確切的網址進行掃描。有沒有辦法,我的腳本會提交我想要的數據,因此它會顯示我想要的腳本內容,然後我可以抓取內容?php grabber - 提交腳本

它像

$url = 'myurl'; 
$data=get_data($url); 
$grabbed=strip_tags(get_match('some regex',$data); 

function get_data($url){ 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
function get_match($regex,$content) 
{ 
    preg_match($regex,$content,$matches); 
    return $matches[1]; 
} 

這個偉大的工程,但我需要在該網址上,這將使我的內容訪問該表單submison腳本。那可能嗎?

非常感謝,

Martin。

更新:

<?php 
//url 
$url = "http://data.skga.sk/Tournaments.aspx"; 

//get the page content 
$content = get_data($url); 
echo $content; 



//gets the match content 
function get_match($regex,$content) 
{ 
    preg_match($regex,$content,$matches); 
    return $matches[1]; 
} 

//gets the data from a URL 
function get_data($url) 
{ 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "ctl00%24RightContentPlaceholder%24dpTo=20.10.2012"); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
?> 

這是我現在的代碼。但我仍然不知道如何解決它。當我在瀏覽器中執行http://data.skga.sk/Tournaments.aspx?ctl00%24RightContentPlaceholder%24dpTo=20.10.2012時很好。但是當我運行這個PHP我可以看到默認頁面

+0

http://ligafiriem.eu/grabber/grabber.php這是我的腳本的結果 –

回答

0

可以處理POST請求是這樣的:

curl_setopt($curl_handler, CURLOPT_POST, true); 
curl_setopt($curl_handler, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt($curl_handler, CURLOPT_URL, $url); 

...其中$ url與<form action="[url]" [...] URL和$ POSTDATA是urlencoded的串像「 para1 = val1 & para2 = val2 & ...'。

參見:http://php.net/manual/en/function.curl-setopt.php

例如:您要提交一個表單,如:

<form action="http://example.com/login.php" method="post"> 
    <input type="text" name="name" /> 
    <input type="password" name="password" /> 
    <input type="submit" /> 
</form> 

這是你會做什麼:

<?php 
$curl_handler = curl_init(); 

curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_handler, CURLOPT_POST, true); 
curl_setopt($curl_handler, CURLOPT_POSTFIELDS, "name=admin&password=secret"); 
curl_setopt($curl_handler, CURLOPT_URL, "http://example.com/login.php"); 

$data = curl_exec($curl_handler); 
curl_close($curl_handler); 
?> 
+0

嘿,那是我想抓住的頁面。 http://www.skga.sk/zoznam-turnajov/有日曆是文本框,但我想要發佈Platnost做的數據:它是''表單操作是Tournaments.aspx。我發現整個網址是http://data.skga.sk/Tournaments.aspx。這我設置爲curlopt_url。 ctl00 $ RightContentPlaceholder $ dpTo = 20.19.2012是我的帖子。我在哪裏犯錯誤? –

+0

在這種情況下,您需要發佈每個必填字段。 'ctl00 $ RightContentPlaceholder $ dpTo'是不夠的。確保您還發布了'__VIEWSTATE'的值,這是您在將請求發佈到服務器之前需要抓取/提取的一些長字符串。 'ctl00_RightContentPlaceholder_dpFrom'似乎也與我有關。 – flxapps

+0

好的。但什麼是'每個必填字段'因爲我點擊過濾器有2個默認值ctl00 $ RightContentPlaceholder $ dpTo和ctl00 $ RightContentPlaceholder $ dpFrom。但我的問題是。當我通過curl發佈其唯一的帖子值我發送或實際寫入輸入值... –

0

如果你使用你捲曲可能希望使用curl_setopt($ch, CURLOPT_POST, 1)切換到POST請求並使用curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields)來隨身攜帶您需要包含的任何後期數據。更多關於curl_setopt documentation的捲曲選項。