-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlesearch.py
76 lines (69 loc) · 2.61 KB
/
googlesearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
class GoogleKeywordSearch:
def __init__(self):
self.sites = []
self.searchitems = []
self.result = []
self.baseurl = "https://www.google.com"
# self.browser = self.installProxy("5.202.74.124", "80")
self.browser = webdriver.Chrome()
def installProxy(self, PROXY_HOST, PROXY_PORT):
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
proxstr = PROXY_HOST + ":" + PROXY_PORT
prox.http_proxy = proxstr
prox.socks_proxy = proxstr
prox.ssl_proxy = proxstr
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
return webdriver.Chrome()
def getSearchOccurance(self, url, keyword):
occurance = 0
try:
searchPattern = "site:${url} ${keyword}"
searchText = searchPattern.replace("${url}", url).replace("${keyword}", keyword)
self.browser.get(self.baseurl)
self.browser.find_element_by_xpath("//input[@name='q']").send_keys(searchText)
self.browser.find_element_by_xpath("//input[@name='q']").send_keys(Keys.ENTER)
time.sleep(3)
resultstats = self.browser.find_element_by_xpath("//div[@id='resultStats']").text
occurance = self.getExtractOccurance(resultstats)
except NoSuchElementException as ne:
occurance = 0
print("0")
except Exception as e:
print("ERROR: "+e)
print(url+" : "+keyword+":"+str(occurance))
return occurance
def getExtractOccurance(self, resultstats):
result = 0
startindex = resultstats.find("About")
lastindex = resultstats.find("result")
if (lastindex > 0 and startindex >= 0):
numstr = resultstats[startindex+5:lastindex]
numstr = numstr.replace(",", "")
result = int(numstr)
elif (lastindex > 0 and startindex < 0):
numstr = resultstats[0:lastindex]
numstr = numstr.replace(",", "")
result = int(numstr)
else:
result = 0
return result
def close(self):
self.browser.quit()
def main():
try:
s = GoogleKeywordSearch()
a = s.getExtractOccurance("About 10 result (0.21 seconds) ")
print(a)
except Exception as e:
print(e)
else:
print("Success!")
if __name__ == "__main__":
main()