SQL 撇号导致网络爬虫描述错误
SQL apostrophe resulting in faulty description on a web crawler
我一直在研究网络爬虫。
让我描述一下问题。
假设当您将 url http://whosebug.com
现在,这是错误的部分,假设页面的元描述是:
Jason's Stack Overflow is the largest online community for programmers to learn, share their knowledge, and advance their careers.
每当我 运行 我的爬虫,它一看到撇号就会停止,只爬到 Jason。因此,整个描述显示为:
Jason
这是抓取工具的代码。告诉我出了什么问题,并希望如何解决它,因为它让我发疯。我错过了一些超小的东西...
foreach($url as $k) {
$url = parse_url($k);
if(!isset($url['path'])) {
$selectData = "SELECT * FROM web WHERE url = '$k'";
if(mysql_fetch_row(mysql_query($selectData)) === false) {
$content = getUrl($k);
preg_match('#<title>(.*)</title>#i', $content, $title);
preg_match_all('/<img src=.([`^"\']+)/', $content, $img);
preg_match('/<head>.+<meta name="description" content=.([`^"\']+)/is', $content, $description);
preg_match('/<head>.+<meta name="author" content=.([`^"\']+)/is', $content, $author);
#preg_match_all('/href=.([`^"\']+)/i', $content, $anchor);
preg_match('/<body.*?>(.*?)<\/body>/is', $content, $body);
if(!empty($title[1]) AND !empty($description[1]) || !empty($body[1])) {
echo 'Titlu: '; @print_r($title[1]);
echo "\n";
$body_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$body[0])))); $bodyContent = substr(preg_replace('/\s+/', ' ', $body_trim), 0, 255);
$description_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$description[1])))); $descContent = substr(preg_replace('/\s+/', ' ',$description_trim), 0, 255);
$bodyContent = str_replace('\'', '', $bodyContent);
$descContent = str_replace('\'', '', $descContent);
echo 'Description: '; @print_r($descContent);
echo "\n";
echo 'Author: '; @print_r($author[1]);
echo "\n";
echo 'URL: '; @print_r($k); $date = date("d M Y");
echo "\n";
echo "\n---------------------------------------------------------------------------\n";
$insertData = "INSERT INTO `web` (`url` , `title` , `description` , `body` , `author`, `date`) VALUES ('".$k."', '".@$title[1]."', '".@$descContent."', '".@$bodyContent."', '".@$author[1]."', '".$date."')";
#echo $insertData;
mysql_query($insertData);
}
}
}
}
你得到了精确的 SQL-Injection 大小写,因为你将输入值连接成 SQL 字符串
... VALUES ('".$k."', '".@$title[1]."'...
也许@$title[1](或任何其他变量)' like: "It's SQL Injection" 它会导致 SQL 语法错误,
因为最终 SQL 查询字符串看起来像
VALUES ('something', 'It's SQL Injection', ...
所以实际上你遇到了 SQL 语法错误
如@Bramar 所说 - 使用带参数的准备语句。它将保证您免受真正的 SQL 注入,并将正确处理您的值中的任何 SQL 保留字符
我一直在研究网络爬虫。 让我描述一下问题。
假设当您将 url http://whosebug.com
现在,这是错误的部分,假设页面的元描述是:
Jason's Stack Overflow is the largest online community for programmers to learn, share their knowledge, and advance their careers.
每当我 运行 我的爬虫,它一看到撇号就会停止,只爬到 Jason。因此,整个描述显示为:
Jason
这是抓取工具的代码。告诉我出了什么问题,并希望如何解决它,因为它让我发疯。我错过了一些超小的东西...
foreach($url as $k) {
$url = parse_url($k);
if(!isset($url['path'])) {
$selectData = "SELECT * FROM web WHERE url = '$k'";
if(mysql_fetch_row(mysql_query($selectData)) === false) {
$content = getUrl($k);
preg_match('#<title>(.*)</title>#i', $content, $title);
preg_match_all('/<img src=.([`^"\']+)/', $content, $img);
preg_match('/<head>.+<meta name="description" content=.([`^"\']+)/is', $content, $description);
preg_match('/<head>.+<meta name="author" content=.([`^"\']+)/is', $content, $author);
#preg_match_all('/href=.([`^"\']+)/i', $content, $anchor);
preg_match('/<body.*?>(.*?)<\/body>/is', $content, $body);
if(!empty($title[1]) AND !empty($description[1]) || !empty($body[1])) {
echo 'Titlu: '; @print_r($title[1]);
echo "\n";
$body_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$body[0])))); $bodyContent = substr(preg_replace('/\s+/', ' ', $body_trim), 0, 255);
$description_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$description[1])))); $descContent = substr(preg_replace('/\s+/', ' ',$description_trim), 0, 255);
$bodyContent = str_replace('\'', '', $bodyContent);
$descContent = str_replace('\'', '', $descContent);
echo 'Description: '; @print_r($descContent);
echo "\n";
echo 'Author: '; @print_r($author[1]);
echo "\n";
echo 'URL: '; @print_r($k); $date = date("d M Y");
echo "\n";
echo "\n---------------------------------------------------------------------------\n";
$insertData = "INSERT INTO `web` (`url` , `title` , `description` , `body` , `author`, `date`) VALUES ('".$k."', '".@$title[1]."', '".@$descContent."', '".@$bodyContent."', '".@$author[1]."', '".$date."')";
#echo $insertData;
mysql_query($insertData);
}
}
}
}
你得到了精确的 SQL-Injection 大小写,因为你将输入值连接成 SQL 字符串
... VALUES ('".$k."', '".@$title[1]."'...
也许@$title[1](或任何其他变量)' like: "It's SQL Injection" 它会导致 SQL 语法错误, 因为最终 SQL 查询字符串看起来像
VALUES ('something', 'It's SQL Injection', ...
所以实际上你遇到了 SQL 语法错误
如@Bramar 所说 - 使用带参数的准备语句。它将保证您免受真正的 SQL 注入,并将正确处理您的值中的任何 SQL 保留字符