建庫建表
create database bookstore character set utf8;
use bookstore;
CREATE TABLE `category` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(255) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `book` (
`bid` int(11) NOT NULL AUTO_INCREMENT,
`bname` varchar(255) NOT NULL,
`bprice` varchar(10) NOT NULL,
`bcid` int(11) NOT NULL,
PRIMARY KEY (`bid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼
把make_txt()
換成save_data()
,create_category()
和create_book()
function save_data($data) {
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) {
global $con;
$sql = "insert into category(`cname`) values(\"{$category_name}\")";
print($sql);
print("\n");
mysqli_query($con, $sql);
return mysqli_insert_id($con);
}
function create_book($bname, $bprice, $bcid) {
global $con;
$sql = "insert into book(`bname`,`bprice`,`bcid`) values(\"{$bname}\",'{$bprice}',{$bcid})";
print($sql);
print("\n");
mysqli_query($con, $sql);
}
複製代碼
最終效果
完整代碼
<?php
require 'vendor/autoload.php';
use QL\QueryList;
$ql = new QueryList();
$con = mysqli_connect('localhost', 'root', 'root', 'bookstore');
function get_category($url) {
global $ql;
$data = $ql->get($url)->rules([
"category_name" => ['#default > div > div > div > aside > div.side_categories > ul > li > ul > li > a', 'text'],
"category_url" => ['#default > div > div > div > aside > div.side_categories > ul > li > ul > li > a', 'href'],
])->queryData();
foreach ($data as $key => $value) {
$value['category_url'] = $url . $value['category_url'];
$data[$key] = $value;
}
return $data;
}
function get_book($url) {
global $ql;
echo $url . "\n";
$data = $ql->get($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) {
global $ql;
$res = $ql->get($url)->find('#default > div > div > div > div > section > div:nth-child(2) > div > ul > li.next > a')->href;
return $res;
}
function make_array($data) {
foreach ($data as $key => $value) {
$value['books'] = get_book($value['category_url']);
$data[$key] = $value;
}
return $data;
}
function save_data($data) {
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) {
global $con;
$sql = "insert into category(`cname`) values(\"{$category_name}\")";
print($sql);
print("\n");
mysqli_query($con, $sql);
return mysqli_insert_id($con);
}
function create_book($bname, $bprice, $bcid) {
global $con;
$sql = "insert into book(`bname`,`bprice`,`bcid`) values(\"{$bname}\",'{$bprice}',{$bcid})";
print($sql);
print("\n");
mysqli_query($con, $sql);
}
$data = make_array(get_category('http://books.toscrape.com/'));
save_data($data);
複製代碼
下一節