help regarding google scraping

alifihri

Newbie
Joined
Apr 9, 2023
Messages
7
Reaction score
1
Hi please I need help regarding scraping google results exactly getting the number of search results for each keyword in excel or google sheets.Capture d’écran 2023-04-06 032246.jpg
here is an example, what I want is the number of each keyword (football, beauty, cinema)
Is there any methods?

Thanks in advance,
 
Python:
import requests
import csv
from bs4 import BeautifulSoup

# Read the keywords from a file
with open("keywords.txt", "r") as file:
    keywords = file.read().splitlines()

# Define the User-Agent header
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

# Create a new CSV file and write the headers
with open("results.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Keyword", "Total Results"])

    # Perform the search for each keyword and write the total number of results to the CSV file
    for keyword in keywords:
        response = requests.get(f"https://www.google.com/search?q={keyword}", headers=headers)
        soup = BeautifulSoup(response.content, "html.parser")
        result_stats = soup.find("div", {"id": "result-stats"})
        if result_stats:
            total_results = result_stats.get_text().split()[1].replace(",", "")
            writer.writerow([keyword, total_results])
            print(f"Keyword: {keyword}, Total Results: {total_results}")
        else:
            writer.writerow([keyword, "Not found"])
            print(f"Keyword: {keyword}, Total Results: Not found")


Code:
keywords.txt (input file)
football
beauty
cinema
cats


results.csv (output file)
Keyword,Total Results
football,3000000000
beauty,6200000000
cinema,2660000000
cats,5720000000
 
Python:
import requests
import csv
from bs4 import BeautifulSoup

# Read the keywords from a file
with open("keywords.txt", "r") as file:
    keywords = file.read().splitlines()

# Define the User-Agent header
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

# Create a new CSV file and write the headers
with open("results.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Keyword", "Total Results"])

    # Perform the search for each keyword and write the total number of results to the CSV file
    for keyword in keywords:
        response = requests.get(f"https://www.google.com/search?q={keyword}", headers=headers)
        soup = BeautifulSoup(response.content, "html.parser")
        result_stats = soup.find("div", {"id": "result-stats"})
        if result_stats:
            total_results = result_stats.get_text().split()[1].replace(",", "")
            writer.writerow([keyword, total_results])
            print(f"Keyword: {keyword}, Total Results: {total_results}")
        else:
            writer.writerow([keyword, "Not found"])
            print(f"Keyword: {keyword}, Total Results: Not found")


Code:
keywords.txt (input file)
football
beauty
cinema
cats


results.csv (output file)
Keyword,Total Results
football,3000000000
beauty,6200000000
cinema,2660000000
cats,5720000000
Thanks, is there any ways to integrate this with excel or google sheets?
 
It's a CSV what that script provides

https://developers.google.com/apps-script/samples/automations/import-csv-sheets?hl=en
Thanks, I got it and it works, but after searching for 50 keywords almost it shows NOT FOUND in the results, is it limited?
 
Thanks, I got it and it works, but after searching for 50 keywords almost it shows NOT FOUND in the results, is it limited?
If you're performing all 50 of those quests from 1 IP, Google has probably noticed that you're a bot and blocked you (BeautifulSoup does not execute javascript without being paired with something like Selenium, so it's quite easy to identify).
 
If you're performing all 50 of those quests from 1 IP, Google has probably noticed that you're a bot and blocked you (BeautifulSoup does not execute javascript without being paired with something like Selenium, so it's quite easy to identify).
Are there any free proxies to use, and how to integrate them into the code?
 
Back
Top