先看看網站
步驟
- 書寫代碼
- chrome小彩蛋, 快速生成css選擇器
番外1, code runner 插件設置, 如何從終端輸出
ctrl+shift+p
settings
- 選擇
open user settings
番外2, 設置vscode的默認終端
php代碼
<?php
require 'vendor/autoload.php';
use QL\QueryList;
$ql = new QueryList();
function get_tilte_date($url){
global $ql;
return $ql->get($url)->rules([
'date' => ['header > div > a > time.entry-date.published','text'],
'title' => ['header > h2 > a','text'],
'url'=>['header > h2 > a','href']
])->queryData();
}
function get_content($url){
global $ql;
return $ql->get($url)->find('article.post.type-post.status-publish.format-standard.hentry.category-uncategorized')->text();
}
function make_markdown($content_array){
$md_obj = fopen('mymd.md','w+');
foreach ($content_array as $key => $value) {
fwrite($md_obj,"## {$value['title']}\n");
fwrite($md_obj,"創做時間: `{$value['date']}`\n");
fwrite($md_obj,"```\n");
fwrite($md_obj,"{$value['content']}`\n");
fwrite($md_obj,"```\n");
}
fclose($md_obj);
}
function start($url){
$data = get_tilte_date($url);
foreach ($data as $key => $value) {
$data[$key]['content'] = get_content($value['url']);
}
make_markdown($data);
}
start("https://wordpress-edu-3autumn.localprod.forc.work/");
複製代碼
留個小做業
- 爬取分類下的圖書名和對應價格, 保存到books.txt
- books.toscrape.com
- 最終效果...
小彩蛋(點擊調轉到函數定義的位置)
下一節