abdelrahmanmedhat/blogsscraper

此软件包可以帮助您同步来自互联网上热门博客的文章。

dev-master 2021-12-31 08:29 UTC

This package is auto-updated.

Last update: 2024-09-29 06:20:23 UTC


README

此软件包可以帮助您同步来自互联网上热门博客的文章。

­ Logo

作者

1 ) 安装

1) 安装软件包

在终端中运行以下命令以获取laravel项目的基准路径

composer require abdelrahmanmedhat/blogsscraper

2) 添加服务提供商

打开config/app.php,然后将其添加到providers数组中

AbdelrahmanMedhat\BlogsScraper\BlogsScraperServiceProvider::class

3) 在项目中发布软件包

在终端中运行以下命令以获取laravel项目的基准路径

php artisan vendor:publish --provider="AbdelrahmanMedhat\BlogsScraper\BlogsScraperServiceProvider"

4) 连接到数据库

确保您已将laravel项目与数据库连接

5) 在数据库中发布软件包表

在项目的基准路径中运行以下命令

php artisan migrate

2 ) 解析器

1) 解析器文件夹

在这个文件夹 app/Parsers 中,我们将放置您通过artisan命令创建的任何解析器。

2) 创建您的第一个解析器

• 在项目的基准路径上打开终端。

• 复制以下命令并在终端中粘贴。

• 将命令中的 blog-name 替换为您要抓取数据的博客名称。

• 运行以下命令。

• 您将在路径 app/Parsers 的 Parsers 文件夹中看到新的解析器。

php artisan create:parser blog-name

3) 解析器架构

• 进入路径: app/Parsers .

• 打开此解析器 scitechdailyParser.php .

在这个变量中,您需要定义博客名称。

public static $blogName = 'SciTechDaily';

­

­

在这个变量中,您需要定义主页博客的URL。

public static $blogUrl = 'https://scitechdaily.com';

­

­

在这个变量中,您需要定义博客标志。

public static $blogLogo = 'https://scitechdaily.com/images/cropped-scitechdaily-amp60.png';

­

在下面的变量中,您需要定义我们将从中抓取数据的博客查询

并且您需要将:{{tag}} 替换为标签名称,将:{{page}} 替换为页面编号。

在: https://scitechdaily.com/news/technology/amp/page/2/ 之前

在: https://scitechdaily.com/news/{{tag}}/amp/page/{{page}}/ 之后

    public static $blogQuery = 'https://scitechdaily.com/news/{{tag}}/amp/page/{{page}}/';

­

­

在这个函数中,您需要定义所有帖子HTML节点,这些节点位于帖子HTML div的类中。

    public function posts($html){
        return $html->filter('.listing-item');
    }

| 注意 | => $html 这将返回帖子页面的HTML。

| 注意 | => 您可以使用 ->filter('selector here')$html 或从 $node 中提取其他节点或文本或...等。

­

在这个函数中,您需要定义帖子链接。

    public function postLink($node){
        return $node->filter('.post-title a')->attr('href');
    }

| 注意 | => $node 这将返回帖子页面的HTML。

| 注意 | => 您可以使用 ->attr('attribute here') 从属性中获取值,就像我们在上面的href中做的那样。

­

在这个函数中,您需要定义帖子摘要。

    public function postExcerpt($node){
        return $node->filter('.post-excerpt p')->text();
    }

| 注意 | => 您可以使用 ->text() 从HTML元素中获取文本,就像我们在上面做的那样。

­

在这个函数中,您需要定义帖子标题。

    public function postTitle($node){
        return $node->filter('.post-title a')->text();
    }

­

­

在这个函数中,您需要定义帖子图像。

    public function postImage($node){
        return $node->filter('.post-thumbnail amp-img')->attr('src');
    }

­

­

在这个函数中,您需要定义帖子类别。

    public function postCategory($node){
        return $node->filter('.post-categories li a')->text();
    }

­

­

在这个函数中,您需要定义帖子作者。

    public function postAuthor($post){
        return $post->filter('.post-author')->text();
    }

| 注意 | => $post 这将返回帖子内页的HTML。

­

在这个函数中,您需要定义文章内容。

我们在文章内容中将所有的 amp-img 替换为 img,因为我们从博客中抓取的内容,将图片标签替换为 amp-img。如果我们使用 amp-img,图片将无法在您的博客中加载。因此,我们需要将其更改为 img 标签以便浏览器可以加载。

    public function postContent($post){
        return str_replace('amp-img', 'img', $post->filter('.post-content')->html());
    }

| 注意 | => 您可以使用 ->html() 获取节点的 HTML 值。

­

在这个函数中,您需要定义文章标签。

    public function postTags($post){
        $GLOBALS['postTags'] = [];

        $post->filter('.tags a')->each(function ($tagsNodes) {
            $GLOBALS['postTags'] []= $tagsNodes->text() ;
        });

        return $GLOBALS['postTags'];
    }

| 注意 | => 您可以使用以下方法遍历所有元素:在具有 class .tags 的元素内部有 <a> 标签,就像我们在上面的函数中所做的那样。

    $nodes->each(function ($node) {
        // use $node here
    });

| 注意 | => 您需要以数组形式返回所有标签。

­

­

3) 抓取

同步特定博客的所有文章。

    use AbdelrahmanMedhat\BlogsScraper\BlogsScraper;

    $blog_name = 'scitechdaily';
    $tag_name = 'technology';
    $pages = [1,2,3];

    $BlogsScraper =  new BlogsScraper;
    $BlogsScraper->sync($blog_name,$tag_name,$pages);

| 注意 | => 您也可以使用此脚本在服务器上以特定计划执行定时任务。

­

4) 数据库关系

­

Logo ­

获取所有带关系的文章。

    use AbdelrahmanMedhat\BlogsScraper\Models\Post;

    $posts = Post::with(['category','blog','author','tags'])
    ->simplePaginate(15)->toArray();

­

­

获取所有带关系的博客。

    use AbdelrahmanMedhat\BlogsScraper\Models\Blog;

    $blogs = Blog::with(['posts'])
    ->simplePaginate(15)->toArray();

­

­

获取所有带关系的作者。

    use AbdelrahmanMedhat\BlogsScraper\Models\Author;

    $authors = Author::with(['posts'])
    ->simplePaginate(15)->toArray();

­

­

获取所有带关系的分类。

    use AbdelrahmanMedhat\BlogsScraper\Models\Category;

    $categories = Category::with(['posts'])
    ->simplePaginate(15)->toArray();

­

­

获取所有带关系的标签。

    use AbdelrahmanMedhat\BlogsScraper\Models\Tag;

    $tags = Tag::with(['posts'])
    ->simplePaginate(15)->toArray();

­

­