Curl Web Scraper 问题,数组错误
Curl Web Scraper Problems, wrong Array
我的抓取脚本有问题。我喜欢 select 我的 sql 中的值 sql 一段时间后,将抓取的内容保存在我的数据库中。它可以工作,但是每次第一个请求的内容时,curl 脚本都会向我的数据库发送错误的结果。
我的代码:
<?php
error_reporting(E_ALL);
include('db.php');
$i = 1;
while ($i <= 5)
{
$sql = "SELECT * FROM `plz` WHERE `id` = '$i'";
$row = mysql_fetch_assoc( mysql_query($sql) );
$plz = $row['plz'];
//create array of data to be posted
$post_data['adv_plz'] = "$plz";
$post_data['finda'] = 'adv';
$post_data['lang'] = 'de_DE';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://www.domain.de/');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
$result = utf8_encode($result);
mysql_query("UPDATE plz SET content = '$result' WHERE id = '$i'");
$i++;
}
?>
这是回显的内容 $post_string
adv_plz=01000&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE&adv_plz=01004&finda=adv&lang=de_DE
我希望有人能帮助我。如果我暂时不尝试脚本,它会完美运行。
在进入 foreach 循环之前尝试重置 $post_items 变量。
$post_items = array();
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_d ....
我的抓取脚本有问题。我喜欢 select 我的 sql 中的值 sql 一段时间后,将抓取的内容保存在我的数据库中。它可以工作,但是每次第一个请求的内容时,curl 脚本都会向我的数据库发送错误的结果。
我的代码:
<?php
error_reporting(E_ALL);
include('db.php');
$i = 1;
while ($i <= 5)
{
$sql = "SELECT * FROM `plz` WHERE `id` = '$i'";
$row = mysql_fetch_assoc( mysql_query($sql) );
$plz = $row['plz'];
//create array of data to be posted
$post_data['adv_plz'] = "$plz";
$post_data['finda'] = 'adv';
$post_data['lang'] = 'de_DE';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://www.domain.de/');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
$result = utf8_encode($result);
mysql_query("UPDATE plz SET content = '$result' WHERE id = '$i'");
$i++;
}
?>
这是回显的内容 $post_string
adv_plz=01000&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE&adv_plz=01004&finda=adv&lang=de_DE
我希望有人能帮助我。如果我暂时不尝试脚本,它会完美运行。
在进入 foreach 循环之前尝试重置 $post_items 变量。
$post_items = array();
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_d ....