Logo

dev-resources.site

for different kinds of informations.

Implement SEO Cron Jobs with Replit and SerpApi Code

Published at
3/1/2023
Categories
python
replit
api
seo
Author
dmitryzub
Categories
4 categories in total
python
open
replit
open
api
open
seo
open
Author
9 person written this
dmitryzub
open
Implement SEO Cron Jobs with Replit and SerpApi Code

Intro

This blog post will go over how to use an online IDE called Replit and how it could be used as a host option to host your SerpApi code to run cron jobs for your SEO, data extraction script, or anything else.

What is SerpApi?

SerpApi provides a convenient gateway to access raw data from Google and other search engines through JSON responses, meaning whatever you see in Google or other engines you can see in the response JSON.

It has support for 9 languages (and 2 coming soon, Swift and C++). Playground page to play around and experiment.

But why Replit?

Well, it's simpler from pretty much all sides than other similar tools (such as pythonanywhere) and it keeps only getting better and better, at least for now.

A few words about "Always on" feature

Please keep in mind that this example requires at least a Hacker plan that Replit offers, which currently costs $7/month. This plan is required to use of the "Always on" feature. The Replit plan costs $2 more than a pythonanywhere hacker plan, I think it's worth it.

Setting up Replit account and Repl

First, make sure you've registered an account, and then choose a Hacker plan:

image

Next, we need to create a repl where all code will be added:

image

Choose Python as a Template, name your repl however you like, make it public or private, and create the repl:

image

After the repl has been created, here are the main things we'll be touching, the same as in your IDE or text editor pretty much:

image

Install Packages

Now to the code itself. Add google-search-results under the "Packages" tool and install it:

image

Add a schedule package that will do a Cron job:

image

Code

Here's a very simple example code that runs every 5 seconds until N time and extracts the position of the target website.

Have a look at how the following example runs on Replit, in case you want (you need to change os.getenv("API_KEY") to your SerpApi key).

from serpapi import GoogleSearch
import os
import time
from datetime import datetime, time as datetime_time
import schedule


def job():
    TARGET_KEYWORD = 'coffee'
    TARGET_WEBSITE = 'healthline.com'
    QUERY = 'coffee tips'

    params = {
        "api_key": os.getenv("API_KEY"),             # your serpapi api key, https://serpapi.com/manage-api-key
        "engine": "google",                          # serpapi pareser engine
        "location": "Austin, Texas, United States",  # location of the search
        "q": QUERY,                                  # search query
        "gl": "us",                                  # country of the search, https://serpapi.com/google-countries
        "hl": "en",                                  # language of the search, https://serpapi.com/google-languages 
        "num": 100                                   # 100 results
    }

        search = GoogleSearch(params)
        results = search.get_dict()

        matched_results = []

        for result in results["organic_results"]:
            if TARGET_KEYWORD in result["title"].lower() and TARGET_WEBSITE in result["link"]:
                matched_results.append({
                    "position": result["position"],
                    "match_title": result["title"],
                    "match_link": result["link"]
                })

        print(datetime.now().strftime("%H:%M"))
        print(matched_results)

if __name__ == "__main__":
    scheduled_off_time = datetime_time(6, 11) # 6 hours, 11 minutes. Repl server local time
    schedule.every(5).seconds.until(scheduled_off_time).do(job)

    while True:
        schedule.run_pending()
        print("job pending โŒ›")
        time.sleep(4)

        current_time = datetime.now().time()

        if current_time > scheduled_off_time:
            schedule.clear()
            print("Done โœ…")
            break
Enter fullscreen mode Exit fullscreen mode

Full execution output

job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
https://serpapi.com/search
06:01
[{'position': 75, 'match_title': '8 Ways to Make Your Coffee Super Healthy - Healthline', 'match_link': 'https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy'}]
job pending โŒ›
job pending โŒ›
job pending โŒ›
Done โœ…
Enter fullscreen mode Exit fullscreen mode

Returned result:

[
   {
      "position":75,
      "match_title":"8 Ways to Make Your Coffee Super Healthy - Healthline",
      "match_link":"https://www.healthline.com/nutrition/8-ways-to-make-your-coffee-super-healthy"
   }
]
Enter fullscreen mode Exit fullscreen mode

The one thing I want to emphasize is that make sure you see what the current time at the repl server is by running the command below.

This is to make sure you see what the server's local time is as it might be different from your local one.

from datetime import datetime
datetime.now().strftime("%H:%M:%S") # or without %S, %M

# 05:35:02
Enter fullscreen mode Exit fullscreen mode

Setting up "Always on" feature

In addition to enabling the "Always on" feature, you can also enable "Boost repl" if you need more RAM, CPU, and storage:

enable-always-on-replit-feature

Yay ๐ŸŽ‰ Now your script will be always active and run until a certain time ๐Ÿ™‚

With this approach, you can add tools such as Google Cloud Storage to extract the data and immediately upload it to the cloud bucket, and a lot other things.


Join us on Twitter | YouTube

Add a Feature Request๐Ÿ’ซ or a Bug๐Ÿž

replit Article's
30 articles in total
Favicon
How to use proxy IP to deal with dynamically changing anti-crawler challenges?
Favicon
Calling all Replit users!
Favicon
Baby meets AI: Creating a health tracking ๐Ÿ‘ถ web-app in 15 min with an AI agent ๐Ÿค–.
Favicon
The Fall of a Community
Favicon
FINDING REPLIT ALTERNATIVE FOR LEARNING CODE
Favicon
I'm come back.
Favicon
Changes to Replit's Starter Plan: Announced Friday
Favicon
Replit Tutorial: How to Code Like a Pro with AI Agents
Favicon
1 chi dars
Favicon
Destiny or Coincidence: Based on Shannon Perry s Experiment.
Favicon
So I completed Replit's free 100 Days of Python course
Favicon
Replit's 100 Days of Code (Days 2-12)
Favicon
AIโ€™s a global affair. But Googleโ€™s Project isnโ€™t there?
Favicon
How to make martech POCs easier
Favicon
่งฃๆฑบ Replit ็Œœ้Œฏๅฅ—ไปถๅฎ‰่ฃ็š„ๅ•้กŒ
Favicon
@TopDown On Replit
Favicon
The State of Replit
Favicon
Implement SEO Cron Jobs with Replit and SerpApi Code
Favicon
How to .go : For
Favicon
How to .go
Favicon
Building a Mortal Kombat Website
Favicon
What i knew about react!(These are the things that I've found challenging so far on my react journey.)
Favicon
How to code almost every programming language in your phone ?
Favicon
Installing ffmpeg in repl.it
Favicon
CRM app with Node.js, Replit, and MongoDB
Favicon
Building a Discord bot with Node.js and Replit
Favicon
Packaging software with Nix
Favicon
Playing with REMIX
Favicon
Best cloud IDEs?
Favicon
Build a smart contract oracle with Solidity, Node.js, and Replit

Featured ones: