-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingCrawler.py
216 lines (189 loc) · 8.66 KB
/
BingCrawler.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 12 07:44:36 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.bing.com/?cc=de')
time.sleep(2)
try:
driver.find_element('xpath','//*[@id="bnp_btn_reject"]').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('[yourpath]\chromedriver.exe')
driver.maximize_window()
driver.get('https://www.bing.com/?cc=de')
WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,'bnp_container')))
driver.find_element('xpath','//*[@id="bnp_btn_reject"]').click()
try:
time.sleep(2)
driver.find_element('xpath','//*[@id="sb_form_q"]')
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])
# The class Crawler with its functions crawls through the bing 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
startpage = 'https://www.bing.com/?cc=de'
if startpage != driver.current_url:
driver.get(startpage)
time.sleep(1)
searchbox = driver.find_element('xpath','//*[@id="sb_form_q"]')
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
adsupper = soup.find_all('div',class_='sb_add sb_adTA')
adslower = soup.find_all('div',class_='sb_add sb_adTA b_adscv')
ads = adsupper + adslower
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('li',class_='b_algo')
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 = ['' for i in range(0,5)]
link = a.find('div',class_='b_adurl').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('a').text
content = ' '.join([x.text for x in a.find('p')][1:])
tags = '; '.join([t.text for t in a.find_all('a') if t.text != ''][3:])
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 = ['' for i in range(0,5)]
link = h.find('div',class_='b_attribution').text
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('a').text
content = ' '.join([c.text for c in h.find('p')]).replace('Web ','')
tags = '; '.join([t.text for t in h.find_all('a') if t.text != ''][3:])
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
# find element by xpath doesn't work with Bing, because it changes from page to page
try:
driver.find_element(By.CSS_SELECTOR,"[title^='Nächste Seite']").click()
time.sleep(1)
except ElementClickInterceptedException:
try:
button = driver.find_element(By.CSS_SELECTOR,"[title^='Nächste Seite']")
driver.execute_script("arguments[0].click();", button)
except Exception as e:
print(repr(e))
find_button = BeautifulSoup(driver.page_source,'lxml')
url = find_button.find('a',class_='sb_pagN sb_pagN_bp b_widePag sb_bp ')['href']
nextpage = 'https://www.bing.com' + url
driver.get(nextpage)
return page
# run the crawler
Crawler()
# 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 = "C:\\Users\\andre\\OneDrive\Desktop\BingSearchResults.xlsx"
with pd.ExcelWriter(path, engine='openpyxl') as writer:
dfAds.to_excel(writer,sheet_name='Advertisements')
dfHits.to_excel(writer,sheet_name='Search_Results')