Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the background process crawling headlines regularly in vue

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to crawl headlines regularly for background processes in vue. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

First, open the back-end project, first create a file background_task.js in the project root directory for fetching third-party API and business logic processing, and create a file background_task.js in the dao layer for database business logic processing. Then go to app.js and introduce the background_task.js file at the top:

Var backgroudTask = require ('. / backgroud_task')

Then encapsulate startBackgroundTask () at the bottom to invoke the scheduled task:

App.startBackgroundTask = function () {backgroudTask.start ();}

Enter the bin/www file and call app.startBackgroundTask ()

Enter the background_task file of the project root directory, write the process start method and stop method, call the runOnce method when starting, and realize the business logic of crawling data in the runOnce method. The process crawls every 15 minutes:

Next, call the API to get the headlines. In the routing layer, our writing method get_news () directly calls the dao layer method, and then calls get_news () in runOnce () to implement all the business logic in the dao layer:

Enter the dao layer and write the get_news () method. First, call the third-party API to obtain the news data, and encapsulate the baseRequest () method before initiating the request:

Next, call a third-party API to obtain news data:

We can restart the service and look at the console output:

You can see that each piece of data returned has a unique uniquekey. We can filter the duplicate data by determining whether the database exists in this article based on the uniquekey parameter:

Determine whether this article exists in the database according to the is_exist parameter, and save it to the database if it does not exist:

When we insert the article data, the status is set to 2, because our own post status is set to 1. The distinguishing status code here is due to the discrepancy between the parameters of the uploaded article and the crawled article, so you need to adapt the front-end interface display and back-end interface. We can check to see if the database inserts data properly:

Next, we go to the API for obtaining article list. Parameters return url and uniquekey:

The list of articles managed by the backend can only return articles for which the current user status is 1, and most of the parameters return url and uniquekey:

Next, go back to the vue project. When you click to read the full text on the home page, if status is 1, you will enter the article details interface normally. If the article status is 2, you will directly jump to the url of the corresponding article:

At this point, we have successfully implemented a process to synchronize third-party news headlines in real time. And the front end has also made a proper adaptation, enriching the content of the home page. Finally, we can take a look at the current home page effect:

This is the end of the headline article on how to achieve regular crawling of background processes in vue. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report