เป็นปัญหาปวดตับจริง ๆ ครับ ค้นหาความรู้จากหลายสำนักแล้วครับ ไม่ลงตัวสักที ครั้งนี้เลยมาขอคตวามรู้อย่างจริงจัง ขอคนเมตตาน่ะครับ ผ่านสังเวียนโปรแกรมมิ่งมาเยอะ แต่ Auto post จนปัญหาจริง ๆ
ตัวอย่าง http://news.mthai.com/world-news/213681.html
ความต้องการ
1. ดึงหัวข้อ จัดอันดับมหาวิทยาลัยสีเขียวโลก มหิดล แชมป์ประเทศไทย ไว้ในฟิลด์ชื่อว่า topic
2. ดึง URL ของรูป มาไว้ในฟิลด์ชื่อว่า photo
3. ดึงเนื้อหา เฉพาะของข่าวนี้ ไว้ในฟิลด์ชื่อ message
ฐานข้อมูลชื่อ MyDB
ตารางชื่อ MyTB
** ต้องการที่จะดึงได้ทุกเว็บทุกหมวด
** ตั้งเวลาในการโพสต์ได้ เช่น วันละกี่โพสต์ เวลาไหนได้บ้าง ประมาณนี้อ่ะครับ
รบกวนขอแนทางทีนะครับ หรือ code ที่ผมเอาไปต่อยอดก็ได้ครับ
ขอบคุณอย่างมากครับ :wanwan017:
ไม่น่าจะยากเหนพวก discuz ทำกันตรึม
สคริปที่จะนำมาใช้ เป็นสคริปที่เขียนเองอ่ะครับ ไม่ได้เขัยนด้วย CMS อย่างเช่น Smf , dizcus , WP อ่ะครับ
เคยแต่ดึงมาทั้งหน้าครับ ทำแต่กับ amazon แล้ว บันทึกลง ฐานข้อมูล
อ้างถึงจาก: mikeyx ใน 21 มกราคม 2013, 09:46:43
เคยแต่ดึงมาทั้งหน้าครับ ทำแต่กับ amazon แล้ว บันทึกลง ฐานข้อมูล
หมายถึงดึงทั้งหน้าเหรอครับ
และส่วนไหนบ้างครับที่เก็บลงฐานข้อมูลน่ะครับ :wanwan020:
ตัวอย่างของเว็บไทยที่ผมทำ
if(time()>=strtotime("00:00:00") && time()<strtotime("20:00:00")){ //ระยะเวลาช่วงที่คนเล่นมาก ๆ ให้เอาข้อมูลจากฐานข้อมูลมาแสดง
$query = mysql_query(.................);
$result = mysql_fetch_array($query);
echo $result[.........];
}else{ // ถ้านอกนั้น
$data=file_get_contents("........................."); //ดึงข้อมูลหน้าที่ต้องการ
แล้ว insert in to $data ลงฐานข้อมูล
และแสดง $data ด้วย
ก่อนจะแสดงก็ให้ตัดพวก html table div script ออกให้หมด เหลือแต่ text เพียว ๆ
}
ดึง html โค้ดมาเก็บในตัวแปรก่อน แล้วใช้พวก regex ครับ
แต่ละคน เทพๆทั้งนั้น :-[
ใช้ curl ดึงมาแล้วก็ค่อยๆตัดแท็กที่ไม่ต้องการออกจนเหลือสิ่งที่ต้องการครับ
ดันนิสนึง :wanwan017:
ใช้ curl ดึงมาแล้วตัดลงเก็บลงฐานข้อมูลคับ
ได้รึยังครับ
ถ้าไม่ได้เดียวส่งให้
อยากได้บ้างจัง ทุกเว็บทุกหน้า... :wanwan020:
อ้างถึงจาก: rnon ใน 21 มกราคม 2013, 20:30:28
ได้รึยังครับ
ถ้าไม่ได้เดียวส่งให้
ยังเลยครับPM ไปแล้วนะครับ
ขอบคุณมากครับ
ถ้าไม่เป็นการรบกวนขอบ้างครับ :wanwan017:
อ้างถึงจาก: zidit ใน 21 มกราคม 2013, 09:58:51
ดึง html โค้ดมาเก็บในตัวแปรก่อน แล้วใช้พวก regex ครับ
ตามนี้ครับ
เท่าที่รู้มาใช้ str replace และ Regular Expression ครับ
ในการกำหนดดึงเป็นส่วนๆ เช่นหัวข้อ เนื้อหา ภาพ
แล้วจากนั้นก็ดึงเก็บลงฐานข้อมูลครับ ของเอ็มไทยง่ายหน่อย มี feed
ถ้ายังไงสนใจโค้ด เดียว PM ไปให้ครับ โค้ดสั้นมากก
curl เก็บ content ของเว็บเป้าหมายค่ะ
ส่วนจะให้ auto post โดยเอาลง db ก่อน คงต้องอาศัย crontab
หรือไม่ก็ศึกษา xml rpc ยิงเข้าไปที่ wordpress ตรงๆ
อ้างถึงจาก: adjobb ใน 22 มกราคม 2013, 00:38:15
เท่าที่รู้มาใช้ str replace และ Regular Expression ครับ
ในการกำหนดดึงเป็นส่วนๆ เช่นหัวข้อ เนื้อหา ภาพ
แล้วจากนั้นก็ดึงเก็บลงฐานข้อมูลครับ ของเอ็มไทยง่ายหน่อย มี feed
ถ้ายังไงสนใจโค้ด เดียว PM ไปให้ครับ โค้ดสั้นมากก
ขอบคุณมากครับ :wanwan017:
PM เข้าดูไม่ได้ในนี้ละกันครับ
ของ mthai ยังไม่ค่อยลองแก้โค้ดดูครับ โค้ดที่เคยแกะคนอื่นมาก็ประมาณนี้ครับ อันนี้เป็นการดึงข่าวจาก ไทยรัฐ นะครับไม่ใช่เอ็มไทย แต่หลักการมันเหมือนๆกัน
ปล. ดึงลง wordpress นะครับลองเอาไปปรับดึงลง ฐานข้อมูลดูครับ
ผมตอบ PM ไม่ได้นะครับ ที่ส่ง ออเร่อมา โฮสน่าจะปิดฟังชั่น อะไรสักอย่าง....
config.php
<?php
/************************/
/* หมวดของการติดต่อฐานข้อมูล */
/**********************/
$host = "mysql.main-hosting.com"; //ใส่ชื้อโฮสของคุณเช่น $host = 'localhost';
$root = "u706496516_jeDaH"; //ใส่ชื่อผู้ใช้เพื่อติดต่อฐานข้อมูลเช่น $root = 'root';
$pass = "XubySejasu"; //ใส่รหัสผ่านเพื่อติดต่อฐานข้อมูลเช่น $pass = 'password';
$dtbn = "u706496516_RyReN"; //ใส่ชื่อฐานข้อมูล WP ของคุณเช่น $dtbn = 'databasename';
$fix = "wp_"; //ใส่ชื่อนำหน้าฐานข้อมูล WP ของคุณ
$wp_userid = "1"; //ใส่ WPUserID ของคุณโดยทั่วไปหากคุณเป็ฯผู้ดูและระบบ WPUserID จะเป็น 1
/********************/
/* หมวดของการตั้งค่าระบบ */
/*******************/
$submitnew_ent = "1";//หากต้องการดึงข่าวบันเทิงให้ใส่ค่าเป็น 1 หากไม่ต้องการให้ใส่เป็น 0
$wp_categoriesID_ent = "4";//ใส่ categoriesID สำหรับข่าวบันเทิง
$objConnect = mysql_connect($host,$root,$pass) or die("Error Connect to Database");
$objDB = mysql_select_db($dtbn);
$cn = @mysql_connect($host,$root,$pass);
mysql_query("SET NAMES UTF8");
?>
ent.php
<?php
include("config.php");
$wp_categoriesID = $wp_categoriesID_ent;
$submitnew = $submitnew_ent;
$xml = "http://www.thairath.co.th/rss/ent.xml";
include("submit.php");
submit.php
<?php
if($submitnew==1){
$filename = $xml;
$dom = new DOMDocument();
if ($dom->load($filename)) {
$data = $dom->getElementsByTagName("item");
foreach($data as $theData) {
$title = trim($theData->getElementsByTagName("title")->item(0)->nodeValue);
$description = trim($theData->getElementsByTagName("description")->item(0)->nodeValue);
$link = trim($theData->getElementsByTagName("link")->item(0)->nodeValue);
$image = trim($theData->getElementsByTagName("enclosure")->item(0)->getAttribute('url'));
$i=0;
$strSQL = "SELECT * FROM ".$fix."posts WHERE post_content_filtered = '$link'";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)) {
$i=++$i;
}
if($i==0){
$contentdata=file_get_contents("$link");
$next_start=0;
$start=strpos($contentdata,'<div id="details" class="page">',$next_start);
if($next_start==0){
$end = strpos($contentdata,'</p></p>',$start);
$content = substr($contentdata,$start,($end-$start));
$next_start=1;
}
$pos1 = explode("<img src=\"",$content);
$content = implode("<img src=\"http://www.thairath.co.th",$pos1);
$pos1 = explode("th /media/",$content);
$content = implode("th/media/",$pos1);
$pos1 = explode("<div class=\"column-420 column-left\">",$content);
$content = implode("",$pos1);
$pos1 = explode("<div class=\"entry\">",$content);
$content = implode("",$pos1);
$pos1 = explode("<div id=\"details\" class=\"page\">",$content);
$content = implode("",$pos1);
$pos1 = explode(" alt=\"\" class=\"detail-image\" />",$content);
$content = implode("",$pos1);
$content = "$content</p></p>";
//echo '<a href="'.$link.'">'.$title."</a><br />";
//echo $content;
$date = date("Y-m-d");
$time = date("H:i:s");
$alias = date("Y-m-d-H-i-s");
$ip = $_SERVER["REMOTE_ADDR"];
$pos1 = explode("'", $title);
$title = implode("\'",$pos1);
$pos2 = explode("'", $content);
$content = implode("\'",$pos2);
$copy = "<br> เนื้อหาจาก<a href=\"$link\" target =\"_blank\">หนังสือพิมพ์ไทยรัฐ</a>";
$content ="$content $copy";
$strSQL3 = "INSERT INTO ".$fix."posts
VALUES ('', '$wp_userid', '$date $time', '$date $time', '$content', '$title','', 'publish', 'open', 'open',
'', '', '','','$date $time','$date $time','$link','0',
'','0','post','','0')";
$objQuery3 = mysql_query($strSQL3) or die ("Error Query [".$strSQL3."]");
$strSQL = "SELECT * FROM ".$fix."posts WHERE post_content_filtered = '$link'";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)) {
$PostID =$objResult["ID"];
}
$strSQL33 = "INSERT INTO ".$fix."term_relationships
VALUES ('$PostID', '$wp_categoriesID', '0')";
$objQuery33 = mysql_query($strSQL33) or die ("Error Query [".$strSQL33."]");
$strSQL5= "UPDATE ".$fix."term_taxonomy SET count = count+1
WHERE term_id = '$wp_categoriesID'";
$objQuery5 = mysql_query($strSQL5) or die ("Error Query [".$strSQL5."]");
}
}
} else {
echo("This document is not valid!");
}
}
?>
ที่บอกว่า error ทาง PM ตะกี้ลองแก้ให้ดูแล้วครับรันผ่าน... ใน localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html{
font:11px Arial, Helvetica, sans-serif; /* Sets the font size and type for the whole html page */
color:#333;} /* Sets the font color for the whole html page */
.menu{
margin:0 auto;
width: 1024px; /*กำหนดความกว้าง*/
background-color: #333;} /* กำหนดสีพื้นหลัง */
.menu ul{
margin: 0;
padding: 0;
float: left;}
.menu ul li{
display: inline;
float: left;
text-decoration: none; /* removes the underline from the menu text */
color: #fff; /* text color of the menu */
padding: 10.5px 11px; /* 10.5px of padding to the right and left of the link and 11px to the top and bottom */
background-color: #333;
width:260px;
} /* Makes the link all appear in one line, rather than on top of each other */
.menu ul li a{
float: left;
text-decoration: none; /* removes the underline from the menu text */
color: #fff; /* text color of the menu */
padding: 10.5px 11px; /* 10.5px of padding to the right and left of the link and 11px to the top and bottom */
background-color: #333;}
.menu ul li a:visited{ /* This bit just makes sure the text color doesn't change once you've visited a link */
color: #fff;
text-decoration: none;}
.menu ul li a:hover, .menu ul li .current{
color: #fff;
background-color:#0b75b2;} /* change the background color of the list item when you hover over it */
</style>
</head>
<body>
<div class="menu">
<ul>
<?
$xml5=file("http://wordpress.in.th/feed");
$xmlDATA="";
foreach($xml5 as $key=>$value){
$xmlDATA.=$value;
}
$data1=explode("<item>",$xmlDATA);
$iTitle=array();
$iLink=array();
$iDesc=array();
$content=array();
foreach($data1 as $key=>$value){
if($key>0 AND $key!=30){
echo "<li>";
$value=str_replace("</item>","",$value);
$iTitle[$key]=mb_substr(strip_tags(substr($value,strpos($value,"<title>"),strpos($value,"</title>"))),0,30,'UTF-8');
$content[$key]=substr($value,strpos($value,"<content:encoded>"),strpos($value,"</content:encoded>")); //<content:encoded>
$iLink[$key]=strip_tags(substr($value,strpos($value,"<link>"),strpos($value,"</link>")-strpos($value,"<link>")));
$iDesc[$key]=strip_tags(substr($value,strpos($value,"<description>"),strpos($value,"</description>")-strpos($value,"<description>")));
preg_match('/src="(.*?)"/', $content[$key], $matches);
//echo $matches[1]."<br>";
echo '<img src="'.$matches[1].'" width="300" height="250"><br>';
echo "<a href='".$iLink[$key]."' target='_blank' />".$iTitle[$key]."....</a><br/>";
//preg_match('/src="(.*?)"/', $content[$key], $matches);
//print_r($matches);
//print_r($matches);
echo "</li>\n";
}
}
?>
</ul>
<br style="clear: left" />
</div>
</body>
</html>
อ้างถึงจาก: adjobb ใน 22 มกราคม 2013, 01:18:07
ที่บอกว่า error ทาง PM ตะกี้ลองแก้ให้ดูแล้วครับรันผ่าน... ใน localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html{
font:11px Arial, Helvetica, sans-serif; /* Sets the font size and type for the whole html page */
color:#333;} /* Sets the font color for the whole html page */
.menu{
margin:0 auto;
width: 1024px; /*กำหนดความกว้าง*/
background-color: #333;} /* กำหนดสีพื้นหลัง */
.menu ul{
margin: 0;
padding: 0;
float: left;}
.menu ul li{
display: inline;
float: left;
text-decoration: none; /* removes the underline from the menu text */
color: #fff; /* text color of the menu */
padding: 10.5px 11px; /* 10.5px of padding to the right and left of the link and 11px to the top and bottom */
background-color: #333;
width:260px;
} /* Makes the link all appear in one line, rather than on top of each other */
.menu ul li a{
float: left;
text-decoration: none; /* removes the underline from the menu text */
color: #fff; /* text color of the menu */
padding: 10.5px 11px; /* 10.5px of padding to the right and left of the link and 11px to the top and bottom */
background-color: #333;}
.menu ul li a:visited{ /* This bit just makes sure the text color doesn't change once you've visited a link */
color: #fff;
text-decoration: none;}
.menu ul li a:hover, .menu ul li .current{
color: #fff;
background-color:#0b75b2;} /* change the background color of the list item when you hover over it */
</style>
</head>
<body>
<div class="menu">
<ul>
<?
$xml5=file("http://wordpress.in.th/feed");
$xmlDATA="";
foreach($xml5 as $key=>$value){
$xmlDATA.=$value;
}
$data1=explode("<item>",$xmlDATA);
$iTitle=array();
$iLink=array();
$iDesc=array();
$content=array();
foreach($data1 as $key=>$value){
if($key>0 AND $key!=30){
echo "<li>";
$value=str_replace("</item>","",$value);
$iTitle[$key]=mb_substr(strip_tags(substr($value,strpos($value,"<title>"),strpos($value,"</title>"))),0,30,'UTF-8');
$content[$key]=substr($value,strpos($value,"<content:encoded>"),strpos($value,"</content:encoded>")); //<content:encoded>
$iLink[$key]=strip_tags(substr($value,strpos($value,"<link>"),strpos($value,"</link>")-strpos($value,"<link>")));
$iDesc[$key]=strip_tags(substr($value,strpos($value,"<description>"),strpos($value,"</description>")-strpos($value,"<description>")));
preg_match('/src="(.*?)"/', $content[$key], $matches);
//echo $matches[1]."<br>";
echo '<img src="'.$matches[1].'" width="300" height="250"><br>';
echo "<a href='".$iLink[$key]."' target='_blank' />".$iTitle[$key]."....</a><br/>";
//preg_match('/src="(.*?)"/', $content[$key], $matches);
//print_r($matches);
//print_r($matches);
echo "</li>\n";
}
}
?>
</ul>
<br style="clear: left" />
</div>
</body>
</html>
สคริปของไทยรัฐ ดึงจาก xml ใช่มั้ยครับ
ถ้าเว็บอื่น เราก็ต้องไปหา rss จากเว็บอื่นด้วยเปล่าครับ :wanwan017:
อันแรกนะครับ ดึงข่าวจากไทยรัฐ....
ส่วนมากเว็บใหญ่ๆมักจะมี rss หรือ feed อยู่แล้วครับ
แต่เอาไปดัดแปลงได้ครับ ดึงส่วน html มา แล้วเอามาตัด
เก็บก่อน ว่างเดี๋ยวมาดู
regexp คือคำตอบ :wanwan003:
เยี่ยมครับ ความรู้ล้วนๆ :wanwan017: