-
Notifications
You must be signed in to change notification settings - Fork 0
/
g_scraper_selenium.py
159 lines (134 loc) · 5.31 KB
/
g_scraper_selenium.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from lib2to3.pgen2 import driver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium_stealth import stealth
from initialize import *
#from manage_DB_MySQL import *
from telegram_bot import *
import time, json
from fake_useragent import UserAgent
from dotenv import load_dotenv
load_dotenv()
docker_url = os.environ.get("docker_1")
create_necessary_folder()
create_db_proxy()
create_db_keywords()
output_json = 'output.json'
debug_log = 'debug.log'
#remaining_keyword_n = get_remaining_keywords_MySQL()
remaining_keyword_n = get_remaining_keywords()
proxy_user = 'ibeppo993'
proxy_pass = 'Ta802Ta802'
# domain = 'www.google.com'
# gl = 'us'
# hl = 'EN'
# uule = 'w+CAIQICINVW5pdGVkIFN0YXRlcw'
domain = 'www.google.it'
gl = 'it'
hl = 'IT'
uule = 'w+CAIQICIFSXRhbHk'
while remaining_keyword_n > 0:
def_date_time = get_now_time()
#keyword = get_keyword_MySQL()
keyword = get_keyword()
print(keyword)
#proxy = get_proxy_MySQL()
proxy = get_proxy()
print(proxy)
# Create a new instance of the Chrome driver
option = webdriver.ChromeOptions()
ua = UserAgent()
userAgent = ua.random
#option.add_argument("--headless")
option.add_argument('--incognito')
#option.add_argument(f'--user-agent={userAgent}')
option.add_argument("--window-size=1920,1080")
option.add_argument("--disable-infobars")
option.add_argument("--disable-popup-blocking")
option.add_argument('--disable-blink-features=AutomationControlled')
print(proxy)
option.add_argument(f'proxy-server={proxy}')
option.add_argument('--no-first-run --no-service-autorun --password-store=basic')
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_argument('--disable-blink-features=AutomationControlled')
option.add_experimental_option('useAutomationExtension', False)
#driver = webdriver.Remote(f'{docker_url}wd/hub',options = option)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option)
# stealth(driver,
# languages=["en-US", "en"],
# vendor="Google Inc.",
# platform="Win32",
# webgl_vendor="Intel Inc.",
# renderer="Intel Iris OpenGL Engine",
# fix_hairline=True,
# )
# driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
# "source": """
# Object.defineProperty(navigator, 'webdriver', {
# get: () => undefined
# })
# """
# })
# Go to the Google home page
url = get_url_to_scrape(keyword, domain, uule, hl, gl)
#driver.get('https://www.whatismyip.com/it/')
#time.sleep(1)
#driver.get('https://bot.sannysoft.com/')
#time.sleep(1)
driver.get(url)
time.sleep(2)
#print('pausa 20 secondi per delay no proxy')
#time.sleep(1)
random_number = random.choice([1,2])
if random_number == 1:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# driver.refresh()
check_captcha = driver.current_url
captcha_url = 'sorry/index?continue='
print(check_captcha)
if captcha_url in check_captcha:
print('------------ richiesta captcha')
print(check_captcha)
#rehab_keyword_MySQL(keyword)
#postpone_proxy_MySQL(proxy)
rehab_keyword(keyword)
postpone_proxy(proxy)
log_telegram = telegram_bot_sendtext(f"{def_date_time}\nkw mancanti {remaining_keyword_n}\n{proxy}\n{keyword}\n{docker_url}\nRichiesta Captcha")
#log
with open(debug_log, 'a') as f:
f.write(f"{proxy};"+time.strftime('%Y%m%d-%H%M%S')+f";Richiesta Captcha;{keyword};{docker_url}\n")
driver.quit()
#remaining_keyword_n = get_remaining_keywords_MySQL()
remaining_keyword_n = get_remaining_keywords()
else:
HTML_DOM = driver.execute_script("return document.documentElement.outerHTML")
keyword_enc = urllib.parse.quote_plus(keyword)
with open(f'html_output/{def_date_time}-{keyword_enc}.html', 'w+') as f:
f.write(HTML_DOM)
f.close()
SERP_dict = [{
"keyword": keyword,
"proxy": proxy,
"url": driver.current_url,
'execution_time': def_date_time,
}]
df = pd.DataFrame.from_dict(SERP_dict)
print(df)
if os.path.isfile(output_json):
df_read = pd.read_json(output_json, orient='index')
df_read = pd.concat([df_read, df], ignore_index=True)
#df_read.drop_duplicates(inplace=True)
df_read.to_json(output_json, orient='index')
else:
df.to_json(output_json, orient='index')
#log
with open(debug_log, 'a') as f:
f.write(f"{proxy};"+time.strftime('%Y%m%d-%H%M%S')+f";Richiesta Completata;{keyword};{docker_url}\n")
driver.quit()
#remaining_keyword_n = get_remaining_keywords_MySQL()
remaining_keyword_n = get_remaining_keywords()
print(remaining_keyword_n)
if int(remaining_keyword_n) % 100 == 0:
log_telegram = telegram_bot_sendtext(f"{def_date_time}\nkw mancanti {remaining_keyword_n}\n{proxy}\n{keyword}\n{docker_url}\nRichiesta Eseguite")