-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonlineVisibilityBroker(old).py
173 lines (152 loc) · 6.59 KB
/
onlineVisibilityBroker(old).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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 28 07:04:48 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 bs4 import BeautifulSoup
import lxml
import time
import numpy as np
import pandas as pd
# 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:
time.sleep(2)
driver.find_element('xpath','//*[@id="W0wltc"]/div').click()
time.sleep(1)
try:
driver.find_element('xpath','/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
except:
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 = [
'agora-direct',
'banxbroker',
'comdirect',
'consorsbank',
'etoro',
'finanzen',
'flatex',
'ing',
'justtrade',
'onvista',
'sbroker',
'smartbroker',
'scalable.capital',
'traderepublic',
'union-investment'
]
keywords= [
'Aktien',
'Börse',
'Depot',
'ETF',
'Wertpapiere'
]
###############################################################################
# Setting the driver and click through the cookie banner
driver = webdriver.Chrome('C:\\Users\\andre\Documents\Python\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()
###############################################################################
# This function crawls through the google search engine and scrapes all the relevant data
def crawlScrape():
dataAds = []
dataHits = []
# Searching for all keywords with a loop
for k in keywords:
print('Searching for: ' + k)
#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)
rankAds = 0
rankHits = 0
page = 1
while page <= 5:
print('page: ' + str(page))
soup = BeautifulSoup(driver.page_source,'lxml')
# Code block for the collection of google ads
ads = soup.find_all('div',class_='uEierd')
for ad in ads:
rankAds += 1
link = ad.find('span',class_='x2VHCd OSrXXb nMdasd qzEoUe').text
for c in companies:
if c + '.de' in link or c + '.com' in link or c + '.net' in link or 'de.' + c in link:
try:
title = ad.find('div',class_='CCgQ5 vCa9Yd QfkTvb MUxGbd v0nnCb').text
except:
title = str(ad.find('div',class_='CCgQ5 vCa9Yd QfkTvb MUxGbd v0nnCb'))
try:
content = ad.find('div',class_='MUxGbd yDYNvb lyLwlc').text
except:
content = ''
try:
tags = ad.find('div',class_='XUpIGb MUxGbd lyLwlc aLF0Z OSrXXb').text
except:
tags = ''
fullcontent = ad.text
dataAds.append([c,k,rankAds,page,title,content,tags,fullcontent,link])
# Code block for the collection of generic search results
hits = soup.find_all('div',class_='MjjYud')
for h in hits:
rankHits += 1
link = h.find('a')['href']
for c in companies:
if c + '.de' in link or c + '.com' in link or c + '.net' in link or 'de.' + c in link:
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
except:
content = ''
fullcontent = h.text
dataHits.append([c,k,rankHits,page,title,content,fullcontent,link])
page += 1
# click on the 'next' button to scrape the next page
driver.find_element('xpath','//*[@id="pnnext"]/span[2]').click()
time.sleep(1)
return [dataAds,dataHits]
data = crawlScrape()
# The cleaned up data can be shown in a dataframe
dfAds = pd.DataFrame(data[0],columns=['company','keyword','rank','page','title','content','tags','fullcontent','link'])
dfHits = pd.DataFrame(data[1],columns=['company','keyword','rank','page','title','content','fullcontent','link'])
# I'm exporting the data into one excel-file and save the dataframes in different sheets
path = '[path to the place where you want to save this file]\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')