pythonstringsearchgoogle-api-python-clientphrase

Python Code to Retrieve Records of Exact Phrase/String for a Google Search Query


There's a similar question but it is not answered.

I am trying to retrieve a list of URL links for the EXACT phrase/string in a Google search query with python code. I do not want search results or URL links of SIMILAR phrases/strings. Hence, my list can contain nothing (or len[MyList] == 0) if there is no EXACT match for my search phrase/string in google search.

For example, the picture shows no result for a Google search for the string "Comparison technique to expose conversation dynamics in meta-analysis."

enter image description here

However, when I run the Python code below with the string "Comparison technique to expose conversation dynamics in meta-analysis." as search query it returns some results in the list and I don't want that. Please how can I modify the code so that only records of the EXACT phrase/string will be returned in the list?

For example, such that the search for the phrase "Comparison technique to expose conversation dynamics in meta-analysis." in ggogle will return an empty list [] as there is no EXACT matching result for the search query/phrase in google. Here is the code:

try:
    from googlesearch import search, quote_plus
except ImportError:
    print("No module named 'google' found")

query = "Comparison technique to expose conversation dynamics in meta-analysis."

result = []

for j in search(query, tld="co.in", num=1, stop=2, pause=2):
    result.append(j)

print(result)

Solution

  • What you want is to use Google dorks intext:"your_search" into your query. The purpose of this functionnality is to search only this strict sentence in websites text. To accomplish that, you only need to include the double quote character (%22 is the url encoded correspondance) in your query :

    try:
        from googlesearch import search
    except ImportError:
        print("No module named 'google' found")
    
    query = 'intext:%22Comparison technique to expose conversation dynamics in meta-analysis.%22'
    
    result = []
    
    for j in search(query, tld="co.in", num=1, stop=2, pause=2):
        result.append(j)
    
    print(result)