我正在使用简单的html dom抓取数据。然而,似乎有一些奇怪的行为。它只添加3条记录,即使它写了6次测试。怎么循环6次,只增加3行?
include('simple_html_dom.php');
$html = file_get_html("http://www.dailydot.com/tags/counter-strike/");
foreach($html->find("//li[@class='span4']") as $element) {
echo "test";
$title = strip_tags($element->find("//a[@class='article-title']/h3", 0));
$img = $element->find("//div[@class='picfx']/a/img[@class='lzy-ld']", 0)->getAttribute('data-original');
$link = $element->find("//a[@class='article-title']", 0)->href;
$date = $element->find("//p[@class='byline']/time", 0)->datetime;
mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
}
答案 0 :(得分:1)
可能是因为它失败3次:D thoses插入不是注射安全的。您应该使用real escape string。如果不这样做,如果任何变量包含简单引用,则代码将失败。 (它允许坏人注入sql命令)
include('simple_html_dom.php');
$html = file_get_html("http://www.dailydot.com/tags/counter-strike/");
foreach($html->find("//li[@class='span4']") as $element) {
$title = mysqli_real_escape_string($con, strip_tags($element->find("//a[@class='article-title']/h3", 0)));
$img = mysqli_real_escape_string($con, $element->find("//div[@class='picfx']/a/img[@class='lzy-ld']", 0)->getAttribute('data-original'));
$link = mysqli_real_escape_string($con, $element->find("//a[@class='article-title']", 0)->href);
$date = mysqli_real_escape_string($con, $element->find("//p[@class='byline']/time", 0)->datetime);
mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
echo "test ".mysqli_error($con);
}