-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleCrawler.py
217 lines (191 loc) · 8.66 KB
/
GoogleCrawler.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 11 15:42:21 2022
@author: andre
"""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.common.exceptions import *
from bs4 import BeautifulSoup
import lxml
import numpy as np
import pandas as pd
import time
import re
# Just in case a driver bug happens (Window closes unintended)
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# Alternative setting options
def fixDriverbug():
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get('https://www.google.de/?hl=de')
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,'jw8mI')))
try:
driver.find_element('xpath','//*[@id="W0wltc"]/div').click()
time.sleep(1)
except:
try:
driver.find_element('xpath','/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
except Exception as e:
print(repr(e))
print('There is something wrong with the page')
time.sleep(1)
driver.close()
exit()
###############################################################################
# Setting the companies and keywords I'm searching for
# Import company names from their url
companies = [
'bosch-thermotechnology',
'buderus',
'daikin',
'dimplex',
'mitsubishi-les',
'nibe',
'stiebel-eltron',
'vaillant',
'viessmann',
'wolf'
]
keywords= [
'Wärmepumpe',
'Wärmepumpe Kosten',
'Luftwärmepumpe',
'Wasser Wärmepumpe',
'Erdwärmepumpe'
]
###############################################################################
# Setting the driver and click through the cookie banner
driver = webdriver.Chrome('[path to your driver]\chromedriver.exe')
driver.maximize_window()
driver.get('https://www.google.de/?hl=de')
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,'jw8mI')))
driver.find_element('xpath','//*[@id="W0wltc"]/div').click()
try:
time.sleep(2)
driver.find_element('xpath','/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
except:
fixDriverbug()
###############################################################################
# The two classes Ads and Hits are collecting all the selected search results and save them in a list
class Ads():
adList = []
def appendAdList(self,keyword,company,rank,page,title,content,tags,link,fullcontent):
self.adList.append([keyword,company,rank,page,title,content,tags,link,fullcontent])
# optional
def getList(self):
return self.adList
class Hits():
hitList = []
def appendHitList(self,keyword,company,rank,page,title,content,tags,link,fullcontent):
self.hitList.append([keyword,company,rank,page,title,content,tags,link,fullcontent])
# This function crawls through the google search engine and scrapes all the relevant data
class Crawler():
def __init__(self):
for k in keywords:
print(k) # if you want to check the program while it runs
#this should always be the startpage
driver.get('https://www.google.de/?hl=de')
time.sleep(1)
searchbox = driver.find_element('xpath','/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
searchbox.clear()
searchbox.send_keys(k)
searchbox.send_keys(Keys.ENTER)
time.sleep(1)
self.rankads = 0
self.rankhits = 0
self.page = 1
while self.page <= 10:
soup = BeautifulSoup(driver.page_source,'lxml')
print('page: ' + str(self.page)) # if you want to check the program while it runs
# Code block for the collection of google ads
ads = soup.find_all('div',class_='uEierd')
if ads == '':
continue
for a in ads:
self.rankads = self.rankads +1
self.scrapeAds(k,self.rankads,self.page,a)
# Code block for the collection of generic search results
hits = soup.find_all('div',class_='MjjYud')
if hits == '':
continue
for h in hits:
self.rankhits = self.rankhits +1
self.scrapeHits(k,self.rankhits,self.page,h)
# Go to the next page after the scraping process is finished
self.page = self.newpage(self.page)
def scrapeAds(self,keyword,rank,page,a):
# resetting the variables
link,title,content,tags,fullcontent = np.array(['' for i in range(0,5)])
link = a.find('span',class_='x2VHCd OSrXXb nMdasd qzEoUe').text
for c in companies:
# print(c) # if you want to check the program while it runs
if c + '.de' in link or c + '.com' in link or c + '.eu' in link or 'de.' + c in link:
fullcontent_raw = a.text
fullcontent = re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ', fullcontent_raw)
try:
title = a.find('span').text
content = a.find('div',class_='MUxGbd yDYNvb lyLwlc').text
tags = a.find('div',class_='dcuivd MUxGbd lyLwlc aLF0Z OSrXXb')
if len(tags) > 1:
try:
tags = [re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ',t.text) for t in tags]
except:
pass
except:
pass
Ads().appendAdList(keyword,c,rank,page,title,content,tags,link,fullcontent)
def scrapeHits(self,keyword,rank,page,h):
# resetting the variables
link,title,content,tags,fullcontent = np.array(['' for i in range(0,5)])
link = h.find('a')['href']
for c in companies:
if c + '.de' in link or c + '.com' in link or c + '.eu' in link or 'de.' + c in link:
fullcontent_raw = h.text
fullcontent = re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ', fullcontent_raw)
try:
title = h.find('h3',class_='LC20lb MBeuO DKV0Md').text
except:
str(h.find('h3',class_='LC20lb MBeuO DKV0Md'))
try:
content = h.find('div',class_='VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc lEBKkf').text
tags = h.find('div',class_='HiHjCd')
if len(tags) > 1:
try:
tags = [re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ',t.text) for t in tags]
except:
pass
except:
pass
Hits().appendHitList(keyword,c,rank,page,title,content,tags,link,fullcontent)
def newpage(self,page):
page += 1
if page > 10:
return page
# click on the 'next' button to scrape the next page
driver.find_element('xpath','//*[@id="pnnext"]/span[2]').click()
time.sleep(1)
return page
# run the crawler
Crawler()
# direct output
#print(Ads().adList)
#print(Hits().hitList)
#print(Ads().getList())
###############################################################################
# The cleaned up data can be saved in dataframes
headerAds = ['keyword','company','rank','page','title','content','tags','link','fullcontent']
dfAds = pd.DataFrame(Ads().adList,columns=headerAds)
headerHits = ['keyword','company','rank','page','title','content','tags','link','fullcontent']
dfHits = pd.DataFrame(Hits().hitList,columns=headerHits)
# I'm exporting the data into one excel-file and save the dataframes in different sheets
path = "googleSearchResults.xlsx"
with pd.ExcelWriter(path, engine='openpyxl') as writer:
dfAds.to_excel(writer,sheet_name='Advertisements')
dfHits.to_excel(writer,sheet_name='Search_Results')