我真的很想有人花一点时间看看我的代码。我正在解析一些新闻内容,并且可以将最初的解析插入到包含新闻网址和标题的数据库中。我想进一步扩展它,传递每篇文章链接并解析文章的内容并将其包含在我的数据库中。初步分析工作完全是这样的:如何将任意数量的值绑定到mysqli中的预处理语句?
<?php
include_once ('connect_to_mysql.php');
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$items[] = '("'.mysql_real_escape_string($m->plaintext).'",
"'.mysql_real_escape_string($m->href).'")';
}
$reverse = array_reverse($items);
mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES
".(implode(',', $reverse))."");
?>
正如你所看到的,我使用的是PHP Simple HTML DOM Parser.扩大,我尝试使用mysqli的说法,我可以绑定参数,这样所有的HTML标签插入到了我的数据库。我之前用XML解析完成了这个。问题是我不知道如何将数组绑定,看看我的代码是否正确,是否会以这种方式工作......这里是整个代码:
<?php
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query("SET NAMES 'utf8'");
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
//find main news
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$h = file_get_html('http://www.basket-planet.com'.$m->href.'');
$article = $h->find('div[class=newsItem]');
//convert to string to be able to modify content
$a = str_get_html(implode("\n", (array)$article));
if(isset($a->find('img'))){
foreach ($a->find('img') as $img){
$img->outertext = '';}} //get rid of images
if(isset($a->find('a'))){
foreach ($a->find('a') as $link){
$link->href = 'javascript:;';
$link->target = '';}} //get rid of any javascript
if(isset($a->find('iframe'))){
foreach ($a->find ('iframe') as $frame){
$frame->outertext = '';}} //get rid of iframes
@$a->find('object', 0)->outertext = '';
@$a->find('object', 1)->outertext = '';
//modify some more to retrieve only text content
//put entire content into a div (will if statements work here???)
$text_content = '<div>'.$a.'<br>'.
($a->find('object', 0)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 0)->data.'">Play Video</a> ')
($a->find('object', 1)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 1)->data.'">Play Video</a> ')
($a->find('iframe[src*=youtube]', 0)->src > 0 ? '<a target="_blank" href="'.$a->find('iframe', 0)->src.'">Play Video</a> ')
//couple more checks to see if video links are present
.'</div>';
$items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';
}
//reverse the array so the latest items have the last id
$reverse = array_reverse($items);
$stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");
$stmt->bind_param ???; //(implode(',', $reverse));
$stmt->execute();
$stmt->close();
?>
因此,逻辑是对的每一个HREF一篇文章发现,我通过它来解析内容,我试图将它添加到数组中。我可能有很多错误,但是我还不能测试它,因为我不知道如何绑定它来查看它是否有效。而且我也不确定是否可以在$ text_content div内执行if语句...意思是显示“播放视频”(如果存在)。所以请,如果有人可以花时间与我一起工作,我会很感激。
UPDATE:将if语句更改为$ text_content div中的比较运算符。
格式太糟糕了..我没有看到'foreach($ main'已关闭..请查看[PSR-2-coding-style](https://github.com/php-图/无花果标准/ blob /主/接受/ PSR-2-coding-style-guide.md) – bitWorking 2013-04-10 18:15:09
我关闭了它...你可以请看看代码?foreach关闭后,一切都被输入到$ items数组 – user2025469 2013-04-10 18:18:51