做業代碼
<?php
require 'vendor/autoload.php';
use Medoo\Medoo;
use QL\QueryList;
$ql = new QueryList();
$database = new Medoo([
'database_type' => 'mysql',
'database_name' => 'bookstore',
'server' => 'localhost',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
]);
function get_html_source($url) {
$result = false;
while (!$result) {
$targetUrl = $url;
$proxyServer = "http://http-dyn.abuyun.com:9020";
$proxyUser = "H19D75L76VK89Q8D";
$proxyPass = "8C17B0A80F475BD8";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
if (!$result) {
sleep(3);
}
}
return $result;
}
function get_category($url) {
echo "function get_category is running.... \n";
global $ql;
$data = $ql->html(get_html_source($url))->rules([
"category_name" => ['#default > div > div > div > aside > div.side_categories > ul > li > ul > li:nth-child(2) > a', 'text'],
"category_url" => ['#default > div > div > div > aside > div.side_categories > ul > li > ul > li:nth-child(2) > a', 'href'],
])->queryData();
foreach ($data as $key => $value) {
$value['category_url'] = $url . $value['category_url'];
$data[$key] = $value;
}
return $data;
}
function get_book($url) {
echo "function get_book is running.... \n";
global $ql;
echo $url . "\n";
$data = $ql->html(get_html_source($url))->rules([
"book_name" => ['#default > div > div > div > div > section > div:nth-child(2) > ol > li > article > h3 > a', 'title'],
"book_price" => ['#default > div > div > div > div > section > div:nth-child(2) > ol > li> article > div.product_price > p.price_color', 'text'],
])->queryData();
$next = has_next($url);
if ($next) {
$tmp_arr = explode('/', $url);
$tmp_arr[count($tmp_arr) - 1] = $next;
$next_url = implode('/', $tmp_arr);
$data = array_merge($data, get_book($next_url));
}
return $data;
}
function has_next($url) {
echo "function has_next is running.... \n";
global $ql;
$res = $ql->html(get_html_source($url))->find('#default > div > div > div > div > section > div:nth-child(2) > div > ul > li.next > a')->href;
return $res;
}
function make_array($data) {
echo "function make_array is running.... \n";
foreach ($data as $key => $value) {
$value['books'] = get_book($value['category_url']);
$data[$key] = $value;
}
return $data;
}
function save_data($data) {
echo "function save_data is running.... \n";
foreach ($data as $key => $value) {
$bcid = create_category($value['category_name']);
foreach ($value['books'] as $k => $book) {
$bname = $book['book_name'];
$bprice = $book['book_price'];
create_book($bname, $bprice, $bcid);
}
}
}
function create_category($category_name) {
echo "function create_category is running.... \n";
global $database;
$id = $database->insert('category', [
'cname' => $category_name,
]);
return $database->id();
}
function create_book($bname, $bprice, $bcid) {
echo "function create_book is running.... \n";
global $database;
$database->insert('book', [
'bname' => $bname,
'bprice' => $bprice,
'bcid' => $bcid,
]);
}
$data = make_array(get_category('http://books.toscrape.com/'));
save_data($data);
複製代碼