테스트 사이트 - 개발 중인 베타 버전입니다

신문 기사를 긁어오기^^

· 19년 전 · 2940 · 3
<?

function get($url){//문서가져옮
$fp = fopen($url, "r");

while(!feof($fp)){
$tmp = fgets($fp, 900);
$temp .= $tmp;
}

fclose($fp);

return $temp;
}//function get

function Strip($all){// 링크랑 제목만 빼오기

$link_pm=strpos($all,">");

$link=substr($all,0,$link_pm);

if(!$link) return;

if(ereg("\"",$link)){

$link_p=strpos($link,"\"");
$link=substr($link,$link_p+1);
$link_p=strpos($link,"\"");

$link=substr($link,0,$link_p);
}

if(ereg("'",$link)){

$link_p=strpos($link,"'");
$link=substr($link,$link_p+1);
$link_p=strpos($link,"'");

$link=substr($link,0,$link_p);
}

$link=trim($link);

$substr=substr($all,$link_pm+1);
$substr_p=strpos($substr,"</a>");
$substr=substr($substr,0,$substr_p);
$strip=strip_tags($substr);
$strip=trim($strip);

return array(link => $link, sub => $strip);
}//function strip($all)

function makeUrl($Url, $Link){

if(!strpos($Link,"http://") && substr_count("$Link","http://")) return $Link;

$eURL = pathinfo("$Url");

$host = parse_url($Url);
$host = $host[scheme]."://".$host[host];

$Link = "/".$Link; //링크가 루트인지 확인위해 임의로 붙인다.
if(substr_count("$Link","../")){ //한칸앞으로

$Link = str_replace("./../", "", $Link);
$Link = str_replace("../", "", $Link);

$pos = strrpos($eURL["dirname"], "/");
$makeUrl = substr($eURL["dirname"],0,$pos);

$makeUrl = "$makeUrl"."$Link";

}else if(substr_count("$Link","//") && !strpos($Link, "//")){ //루트로 가라

$makeUrl = $host."/".str_replace("//", "", $Link);

}else{ //제자리

$Link = str_replace("./", "", $Link);
$makeUrl = $eURL["dirname"]."$Link";
}

return $makeUrl;
}
////////////////////////////////////// 펑션끝
$date_Y = date("Y");
$date_M = date("m");
$date_D = date("d");

$mdate = "$date_Y.$date_M.$date_D"; //확장성 위해
////////////////////////////////////////////////////////////<== 신문사별설정 추가가능

$newspaper[] = "동아일보";
$URL[] = "http://www.donga.com/fbin/output?f=total";
$like[] = "$date_Y$date_M$date_D";

$newspaper[] = "연합신문";
$URL[] = "http://www.yonhapnews.co.kr/services/0400000000.html";
$like[] = "$date_Y$date_M$date_D";

////////////////////////////////////////////////////////////<== 여기까지 신문사별설정

for($x=0; $x < sizeof($newspaper); $x++){

$temp = get($URL[$x]);

$all = split("href =|HREF =|href=|HREF=",$temp);//--> url 추출

for($i=0; $i < sizeof($all); $i++){

$kisa = Strip($all[$i]);

if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub])){

$LinkURL = makeUrl($URL[$x], $kisa[link]);
echo "<a href='$LinkURL'>[$newspaper[$x]]$kisa[sub]</a><br>";
}//if(ereg($like[$x], $kisa[link]) & 20 < strlen($kisa[sub]))
}//for($i=0; $i < sizeof($all); $i++)
}//for($x=0; $x < sizeof($newspaper); $x++)
?><div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]</div>

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 3개

19년 전
기타 신문기사의 주소는 스스로 찾으면 되더군요..
아래는 예를 들었습니다.
////////////////////////////////////////////////////////////<== 신문사별설정 추가가능



$newspaper[] = "<font color=cc0000>굿</font>";
$URL[] = "http://www.hot.co.kr/sports/list.html";
$like[] = "$date_Y$date_M$date_D";



////////////////////////////////////////////////////////////<== 여기까지 신문사별설정
긁어올수 있는 신문기사 주소는 어떻게 찾나요?
특별한 방법이 있는지요?
http://www.zeroboard.com/?mid=study_php&page=13&document_srl=1502717&sort_index=voted_count&order_type=desc

게시글 목록

번호 제목
10156
24771
24770
24769
24768
24767
24766
10155
10148
10143
28668
10140
10139
10137
10135
10134
10133
10130
10129
10128
10127
10126
10122
10121
10117
10116
10114
10113
10102
10098
28667
10097
10095
10090
10088
10083
24761
28660
28658
10082
28655
10071
10069
28651
10060
10053
10052
28649
28644
10050
10046
24749
10040
10030
24748
10028
10016
10010
10008
10003
9999
9991
28638
9988
9987
28635
9986
9980
9975
9971
9968
9966
9965
9961
28621
9956
28619
9952
9940
24746
9939
9938
9935
9934
28618
9932
9931
9930
28617
9929
9928
28615
28614
9927
9926
9925
28613
9924
9923
9922