Script to get the TOP and TRENDING SEARCH RESULTS



Quick Video Reference : https://youtube.com/shorts/ABiXQB4TVzc
 

The below script helps for Youtubers and other social media users to make their post, story or reels to be in top of the search results, as well as be in trending. 

Here we are using Google's toolbar and we are just passing the search query over there and it returns back with XML. 

Google Toolbar Search URL : https://google.com/complete/search?output=toolbar&gl=eg=&q=

Say if you want to search the trending keywords of Digital Marketing

Example: https://google.com/complete/search?output=toolbar&gl=eg=&q=digital%20marketing

It returns back with the below xml 

Now, if you take a look at the suggestion data, those are your targeted words to add in your post,story or even in blog labels




In order to avoid copy and pasting each and every value from the suggestion data, we have written a python script that parses these values and provides you the list of keywords, so that you can add straight away in your label section or post area section


import requests
import xml.etree.ElementTree as ET


## This function uses the XML etree functionality to parse the xml content 
def search_keyword(kw):
    url = 'https://google.com/complete/search?output=toolbar&gl=eg=&q='+str(kw)
    resp = requests.get(url)
    mytree = ET.fromstring(resp.content)
    trend_keywords = []
    for ele in list(mytree):
        trend_keywords.append(ele[0].attrib['data'])
    return trend_keywords
    
search_keyword('Machine Learning')

Output:
['machine learning', 'machine learning definition', 'machine learning algorithms', 'machine learning interview questions', 'machine learning projects', 'machine learning course', 'machine learning is a subset of', 'machine learning tutorial', 'machine learning types', 'machine learning projects for final year']


I hope the functionality is pretty clear.

I have imported xml.etree function and that gets the resp.content into the fromstring function. So, by iterating the tree element, we can access the attrib within the xml tags and that retrieves the text from it.

Our previous post : Generic website data extraction

Get clean text from HTML


No comments