2
我在一起使用curl来拉动html的php脚本,将它复制到新页面并保存页面名称。所有这些工作,但我也想收集页面上的网址,并将它们输入到数据库中。从我的研究来看,DOM看起来是最好的方式。但是,当我在代码中包含DOM时,出现“错误,插入查询失败”。 Here是我获取DOM代码的地方。我怀疑这是一个数据库问题。如何在DOM数据库中插入使用DOM刮掉的链接? (或者我做错了什么?)
DOM,PHP和MySQL对我来说是新的,所以任何评论,指针或建议将是有益的和赞赏。
任何有关整体方法的评论或替代建议也非常受欢迎。我并不完全相信DOM最适合从html中挖掘URL。
<html>
<body>
<?
$urls=explode("\n", $_POST['url']);
$proxies=explode("\n", $_POST['proxy']);
for ($counter = 0; $counter <= 6; $counter++) {
for ($count = 0; $count <= 6; $count++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$urls[$counter]);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY,$proxies[$count]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch);
$curl_scraped_page = curl_exec($ch);
$FileName = rand(0,100000000000);
$FileHandle = fopen($FileName, 'w') or die("can't open file");
fwrite($FileHandle, $curl_scraped_page);
$dom = new DOMDocument();
@$dom->loadHTML($curl_scraped_page);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
$hostname="****";
$username="****";
$password="****";
$dbname="leadturtle";
$usertable="happyturtle";
$con=mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname ,$con);
function storeLink($url) {
$query = "INSERT INTO happyturtle (time, ad1, ad2) VALUES ('$FileName','$url', '$gathered_from')";
mysql_query($query) or die('Error, insert query failed');
}
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
storeLink($url,$target_url);
}
mysql_close($con);
fclose($FileHandle);
curl_close($ch);
echo $FileName;
echo "<br/>";
}
}
?>
</body>
</html>
@ user586011:请奖励帮助过您的人,并接受您的旧问题的答案。谢谢。 – DarkDust 2011-03-21 08:31:48