Skip to content

Instantly share code, notes, and snippets.

@ilovefreesw
Created May 28, 2022 07:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A simple script to get all Google Ads from Google Search results directly. Downloads ad title, link, and description.
from bs4 import BeautifulSoup
import requests, csv
import lxml
from requests.structures import CaseInsensitiveDict
ads = []
key = input("Enter a keyword/domain/brand: ")
headers = CaseInsensitiveDict()
headers["authority"] = "www.google.com"
headers["User-Agent"]: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
headers["accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
headers["accept-language"] = "en-US,en;q=0.9"
headers["cache-control"] = "max-age=0"
headers["cookie"] = "SID=KggpaEyTE7l4AxeJkOoox8FjqNoZmdzectCSFSUNmKE4D1br29p2xiDHyh5KZLNM_k0JOQ.; __Secure-1PSID=KggpaEyTE7l4AxeJkOoox8FjqNoZmdzectCSFSUNmKE4D1brG6IVvOZGVDZBwhmDtbqHZg.; __Secure-3PSID=KggpaEyTE7l4AxeJkOoox8FjqNoZmdzectCSFSUNmKE4D1brdFTR6bjSfPYV4lIEqPlg7w.; HSID=A83ab3uLXbjczC13e; SSID=AvK1tOQd4HSDWFFl_; APISID=NJKGyeuVPJQcnE0W/A9aceC63PUPsCT6EO; SAPISID=SnSsieEZPmV-RPfe/AMjZfROErTGSpcLN7; __Secure-1PAPISID=SnSsieEZPmV-RPfe/AMjZfROErTGSpcLN7; __Secure-3PAPISID=SnSsieEZPmV-RPfe/AMjZfROErTGSpcLN7; 1P_JAR=2022-05-27-12; NID=511=qhk932_clF2_815gfm746w0EhctRcRSHLM9UVN9GgRgp2StxG2Q4-LMykwY6dKmyYYCTU1e7m8uCZzekfQtEeDOiB3Ih8WoP8Y1EwUOz01WsHSa5uNqHKIYofSoH17Lf4oEnKw5RV35qLpskECg13QeqcipqZv_YzB10gX21HruiHdcTNSWoKQshpaLRLbG1hEDIdDezdWU_Jk2lswUeQ2HMUBThQeCtoVHQfZ66XHNZvRvG48soJlPrIMjc7c1UhoHOqgl8g2MGkEl_lAjPBnnaXzRpG3JI7qAnhwCaf1OdJsun; OGPC=748494848-3:; AEC=AakniGO6VhnoolbZtMIMHXIh77ecte_1VItg_HeZICJIBduHo5VfQe5MJb0; mp_52e5e0805583e8a410f1ed50d8e0c049_mixpanel=%7B%22distinct_id%22%3A%20%2218105768ac912b-0de1392e0bcacd-54161253-100200-18105768aca873%22%2C%22%24device_id%22%3A%20%2218105768ac912b-0de1392e0bcacd-54161253-100200-18105768aca873%22%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%2C%22%24search_engine%22%3A%20%22google%22%7D; DV=g0k6MwHaRt8Q8JiFc16AWAbeGLpYEBg; SIDCC=AJi4QfG3B3Im0WYhRWEXRpt67kozRDv_r-sUogFBk7j3g3D9z11Qz7M1MpaC3U9Sr8W8_xSZxw; __Secure-3PSIDCC=AJi4QfHuo9Mec4YCHs-tMwhpnjQ5LgytoaPSADqj1rEkOfWwaL4It5qqLO9-5N4A4vvfS0iAoaM"
html = requests.get('https://www.google.com/search?q='+key, headers=headers)
soup = BeautifulSoup(html.text,'html.parser')
titles=soup.select('div[role="heading"]')
links=soup.select('div[role="link"]')
desc=soup.select('div[class="MUxGbd yDYNvb lyLwlc"]')
for i in range(len(titles)):
info = {
"Title": titles[i].text,
"Description": desc[i].text,
"Link": links[i].text
}
ads.append(info)
with open("ads.csv", 'w+') as file:
csvwriter = csv.DictWriter(file, fieldnames=list(ads[0].keys()))
csvwriter.writeheader()
csvwriter.writerows(ads)
input("Press any key to exit...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment