Skip to content

Instantly share code, notes, and snippets.

@dinhkhanh
Created September 29, 2021 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinhkhanh/9bcae95dce08952890d9f5069bd300d1 to your computer and use it in GitHub Desktop.
Save dinhkhanh/9bcae95dce08952890d9f5069bd300d1 to your computer and use it in GitHub Desktop.
Get Google Search Results
import undetected_chromedriver.v2 as uc
from selenium.webdriver.common.by import By
import re
opts = uc.ChromeOptions()
# opts.add_argument('--headless')
# opts.add_argument('--disable-gpu')
driver = uc.Chrome(options=opts)
keywords = ['Kể', 'từ', 'khi', 'dẫn', 'dắt', 'các', 'đội', 'tuyển', 'Việt', 'Nam', 'cuối', 'năm', '2017,', 'HLV', 'Park', 'chưa', 'từng', 'thua', 'các', 'đối', 'thủ', 'Đông', 'Nam', 'Á.', 'Ông', 'đặc', 'biệt', 'có', 'duyên', 'với', 'Malaysia', 'và', 'Indonesia.', 'Tại', 'vòng', 'loại', 'thứ', 'hai', 'World', 'Cup', '2022,', 'đội', 'tuyển', 'cùng', 'bảng', 'G', 'với', 'hai', 'đối', 'thủ', 'này', 'và', 'toàn', 'thắng', 'cả', 'bốn', 'trận.', 'Điểm', 'bất', 'lợi', 'với', 'Việt', 'Nam', 'là', 'cùng', 'lúc', 'thi', 'đấu', 'nhiều', 'mặt', 'trận.', 'Bên', 'cạnh', 'AFF', 'Cup', '2020,', 'đội', 'tuyển', 'còn', 'phải', 'đã', 'vòng', 'loại', 'thứ', 'ba', 'World', 'Cup', '2022', '-', 'khu', 'vực', 'châu', 'Á.', 'Điều', 'này', 'khiến', 'HLV', 'Park', 'phải', 'tính', 'toán', 'về', 'bài', 'toán', 'lực', 'lượng,', 'trong', 'bối', 'cảnh', 'đang', 'có', 'nhiều', 'cầu', 'thủ', 'bị', 'quá', 'tải', 'hoặc', 'chấn', 'thương.', 'This', 'will', 'work', 'on', 'unixes', 'including', 'OS', 'X,', 'Linux', 'and', 'Windows', '(provided', 'you', 'use', 'ANSICON,', 'or', 'in', 'Windows', '10', 'provided', 'you', 'enable', 'VT100', 'emulation).', 'There', 'are', 'ANSI', 'codes', 'for', 'setting', 'the', 'color,', 'moving', 'the', 'cursor,', 'and', 'more.', "best gaming laptops", "best cars", "best insurances", 'This', 'will', 'work', 'on', 'unixes', 'including', 'OS', 'X,', 'Linux', 'and', 'Windows',
'(provided', 'you', 'use', 'ANSICON,', 'or', 'in', 'Windows', '10', 'provided', 'you', 'enable', 'VT100', 'emulation).', 'There', 'are', 'ANSI', 'codes', 'for', 'setting', 'the', 'color,', 'moving', 'the', 'cursor,', 'and', 'more.', ]
with driver:
driver.get('https://google.com?hl=en_US')
for keyword in keywords:
inpElem = driver.find_element(By.NAME, 'q')
inpElem.clear()
inpElem.send_keys(keyword)
inpElem.submit()
resultElem = driver.find_element(By.ID, 'result-stats')
text = resultElem.text
p = re.compile("([0-9,]+)")
search = p.search(text)
print("keyword '" + keyword + "' has " + search.group(1) + " results")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment