How To Scrape Data From A Google Maps
In this article, we volition discuss how to scrape information similar Names, Ratings, Descriptions, Reviews, addresses, Contact numbers, etc. from google maps using Python.
Modules needed:
- Selenium : Usually, to automate testing, Selenium is used. We can do this for scraping too as the browser automation here helps with interacting javascript involved with clicks, scrolls, motion of data betwixt multiple frames, etc.,
- Web/Chrome Driver: Selenium requires a web driver to interface with the chosen browser. Web drivers interact with a remote web server through a wire protocol which is common to all. You tin can check out and install the web drivers of your browser choice, to establish a connectedness with the web commuter.
Download chrome Commuter from https://chromedriver.chromium.org/dwelling house and then copy/cut the file from your download and paste information technology at your C bulldoze, i.eastward C:\chromedriver_win32\chromedriver.exe. Delight make sure you lot are following this path otherwise it volition through a path error.
Syntax:
For obtaining the title of the place:
review_titles=browser.find_element_by_class_name("x3AX1-LfntMc-header-championship-championship")
print(review_titles.text)
For obtaining the rating of the place:
stars=browser.find_element_by_class_name("aMPvhf-fI6EEc-KVuj8d")
print("The stars of eating place are:",stars.text)
For obtaining the description of the identify:
clarification=browser.find_element_by_class_name("uxOu9-sTGRBb-T3yXSc")
print(description.text)
For obtaining the address of the identify:
address=browser.find_elements_by_class_name("CsEnBe")[0]
impress("Address: ",address.text)
For obtaining the contact number of the place:
phone = browser.find_elements_by_class_name("CsEnBe")[-2]
print("Contact Number: ", phone.text)
For obtaining the reviews of the place:
review=browser.find_elements_by_class_name("OXD3gb")
print("———————— Reviews ——————–")
for j in review:
print(j.text)
Example one:
Scrap Title and rating.
Python3
from
selenium
import
webdriver
from
selenium.webdriver.mutual.action_chains
import
ActionChains
options
=
webdriver.ChromeOptions()
options.add_argument(
'headless'
)
browser
=
webdriver.Chrome(
executable_path
=
"C:\chromedriver_win32\chromedriver.exe"
,
options
=
options)
url
=
["https:
/
/
www.google.com
/
maps
/
identify
/
\
Papa
+
John's
+
Pizza
/
@
40.7936551
,
-
74.0124687
,
17z
/
data
=
!
3m1
!
4b1
!\
4m5
!
3m4
!
1s0x89c2580eaa74451b
:
0x15d743e4f841e5ed
!
8m2
!
3d40
.\
7936551
!
4d
-
74.0124687
", "
https:
/
/
www.google.com
/
maps
/
identify
/
\
Lucky
+
Dhaba
/
@
thirty.653792
,
76.8165233
,
17z
/
data
=
!
3m1
!
4b1
!
4m5
!
3m4
!\
1s0x390feb3e3de1a031
:
0x862036ab85567f75
!
8m2
!
3d30
.
653792
!
4d76
.
818712
"]
i
=
0
for
i
in
range
(
len
(url)):
browser.go(url[i])
championship
=
browser.find_element_by_class_name(
"x3AX1-LfntMc-header-title-title"
)
impress
(i
+
1
,
"-"
, title.text)
stars
=
browser.find_element_by_class_name(
"aMPvhf-fI6EEc-KVuj8d"
)
print
(
"The stars of restaurant are:"
, stars.text)
print
(
"\due north"
)
Output:
1 - Papa Johns Pizza The stars of restaurant are: three.6 ii - Lucky Da Dhaba The stars of restaurant are: 3.8
Case 2:
Scrap Title and address.
Python3
from
selenium
import
webdriver
from
selenium.webdriver.common.action_chains
import
ActionChains
options
=
webdriver.ChromeOptions()
options.add_argument(
'headless'
)
browser
=
webdriver.Chrome(
executable_path
=
"C:\chromedriver_win32\chromedriver.exe"
,
options
=
options)
url
=
["https:
/
/
world wide web.google.com
/
maps
/
place
/
\
Papa
+
John'southward
+
Pizza
/
@
40.7936551
,
-
74.0124687
,
17z
/
data
=
!
3m1
!
4b1
!\
4m5
!
3m4
!
1s0x89c2580eaa74451b
:
0x15d743e4f841e5ed
!
8m2
!
3d40
.\
7936551
!
4d
-
74.0124687
", "
https:
/
/
www.google.com
/
maps
/
place
/
\
Lucky
+
Dhaba
/
@
30.653792
,
76.8165233
,
17z
/
data
=
!
3m1
!
4b1
!
4m5
!
3m4
!\
1s0x390feb3e3de1a031
:
0x862036ab85567f75
!
8m2
!
3d30
.
653792
!
4d76
.
818712
"]
i
=
0
for
i
in
range
(
len
(url)):
browser.go(url[i])
title
=
browser.find_element_by_class_name(
"x3AX1-LfntMc-header-title-title"
)
impress
(i
+
1
,
"-"
, championship.text)
address
=
browser.find_elements_by_class_name(
"CsEnBe"
)[
0
]
print
(
"Address: "
, address.text)
print
(
"\n"
)
Output:
ane – Papa Johns Pizza
Address: 6602 Bergenline Ave, West New York, NJ 07093, The states
2 – Lucky Da Dhaba
Address: shop no.ii, Patiala Road, National Highway 64, Zirakpur, Punjab 140603
Example iii:
Chip Title and contact number.
Python3
from
selenium
import
webdriver
from
selenium.webdriver.common.action_chains
import
ActionChains
options
=
webdriver.ChromeOptions()
options.add_argument(
'headless'
)
browser
=
webdriver.Chrome(
executable_path
=
"C:\chromedriver_win32\chromedriver.exe"
,
options
=
options)
url
=
["https:
/
/
www.google.com
/
maps
/
place
/
\
Papa
+
John'south
+
Pizza
/
@
xl.7936551
,
-
74.0124687
,
17z
/
data
=
!
3m1
!
4b1
!\
4m5
!
3m4
!
1s0x89c2580eaa74451b
:
0x15d743e4f841e5ed
!
8m2
!
3d40
.\
7936551
!
4d
-
74.0124687
", "
https:
/
/
www.google.com
/
maps
/
place
/
\
Lucky
+
Dhaba
/
@
30.653792
,
76.8165233
,
17z
/
data
=
!
3m1
!
4b1
!
4m5
!
3m4
!\
1s0x390feb3e3de1a031
:
0x862036ab85567f75
!
8m2
!
3d30
.
653792
!
4d76
.
818712
"]
i
=
0
for
i
in
range
(
len
(url)):
browser.get(url[i])
title
=
browser.find_element_by_class_name(
"x3AX1-LfntMc-header-title-championship"
)
print
(i
+
i
,
"-"
, title.text)
telephone
=
browser.find_elements_by_class_name(
"CsEnBe"
)[
-
two
]
print
(
"Contact Number: "
, phone.text)
impress
(
"\n"
)
Output:
1 - Papa Johns Pizza Contact Number: +1 201-662-7272 2 - Lucky Da Dhaba Contact Number: 095922 67185
Example four:
Bit Title and reviews.
Python3
from
selenium
import
webdriver
from
selenium.webdriver.mutual.action_chains
import
ActionChains
options
=
webdriver.ChromeOptions()
options.add_argument(
'headless'
)
browser
=
webdriver.Chrome(
executable_path
=
"C:\chromedriver_win32\chromedriver.exe"
,
options
=
options)
url
=
["https:
/
/
world wide web.google.com
/
maps
/
identify
/
\
Papa
+
John'due south
+
Pizza
/
@
forty.7936551
,
-
74.0124687
,
17z
/
data
=
!
3m1
!
4b1
!\
4m5
!
3m4
!
1s0x89c2580eaa74451b
:
0x15d743e4f841e5ed
!
8m2
!
3d40
.\
7936551
!
4d
-
74.0124687
", "
https:
/
/
www.google.com
/
maps
/
place
/
\
Lucky
+
Dhaba
/
@
xxx.653792
,
76.8165233
,
17z
/
data
=
!
3m1
!
4b1
!
4m5
!
3m4
!\
1s0x390feb3e3de1a031
:
0x862036ab85567f75
!
8m2
!
3d30
.
653792
!
4d76
.
818712
"]
i
=
0
for
i
in
range
(
len
(url)):
browser.go(url[i])
title
=
browser.find_element_by_class_name(
"x3AX1-LfntMc-header-title-title"
)
print
(i
+
1
,
"-"
, title.text)
review
=
browser.find_elements_by_class_name(
"OXD3gb"
)
print
(
"------------------------ Reviews --------------------"
)
for
j
in
review:
impress
(j.text)
print
(
"\northward"
)
Output:
1 – Papa Johns Pizza
———————— Reviews ——————–
"The food is and then good and they even make the pizza and so fast, omg."
"He deals with the money likewise helps making the pizza without plastic gloves."
"This is my pizza identify to go, no hassles at all!"
2 – Lucky Da Dhaba
———————— Reviews ——————–
"All-time place for a small group of people, nutrient quality is amazing"
"Nice staff and quick service good quantity and well cooked meal."
"I ordered chicken biryani they served me chicken pulao non Biryani."
Code Implementation:
Python3
from
selenium
import
webdriver
from
selenium.webdriver.common.action_chains
import
ActionChains
options
=
webdriver.ChromeOptions()
options.add_argument(
'headless'
)
browser
=
webdriver.Chrome(
executable_path
=
"C:\chromedriver_win32\chromedriver.exe"
,
options
=
options)
url
=
["https:
/
/
www.google.com
/
maps
/
place
/
\
Papa
+
John'south
+
Pizza
/
@
xl.7936551
,
-
74.0124687
,
17z
/
data
=
!
3m1
!
4b1
!\
4m5
!
3m4
!
1s0x89c2580eaa74451b
:
0x15d743e4f841e5ed
!
8m2
!
3d40
.\
7936551
!
4d
-
74.0124687
", "
https:
/
/
www.google.com
/
maps
/
identify
/
\
Lucky
+
Dhaba
/
@
30.653792
,
76.8165233
,
17z
/
information
=
!
3m1
!
4b1
!
4m5
!
3m4
!\
1s0x390feb3e3de1a031
:
0x862036ab85567f75
!
8m2
!
3d30
.
653792
!
4d76
.
818712
"]
i
=
0
for
i
in
range
(
len
(url)):
browser.get(url[i])
title
=
browser.find_element_by_class_name(
"x3AX1-LfntMc-header-championship-title"
)
print
(i
+
i
,
"-"
, title.text)
stars
=
browser.find_element_by_class_name(
"aMPvhf-fI6EEc-KVuj8d"
)
print
(
"The stars of restaurant are:"
, stars.text)
clarification
=
browser.find_element_by_class_name(
"uxOu9-sTGRBb-T3yXSc"
)
print
(
"Description: "
, clarification.text)
address
=
browser.find_elements_by_class_name(
"CsEnBe"
)[
0
]
print
(
"Address: "
, address.text)
phone
=
browser.find_elements_by_class_name(
"CsEnBe"
)[
-
ii
]
impress
(
"Contact Number: "
, phone.text)
review
=
browser.find_elements_by_class_name(
"OXD3gb"
)
print
(
"------------------------ Reviews --------------------"
)
for
j
in
review:
print
(j.text)
impress
(
"\n"
)
Output:
How To Scrape Data From A Google Maps,
Source: https://www.geeksforgeeks.org/how-to-scrape-data-from-google-maps-using-python/
Posted by: rosadotrah1940.blogspot.com
0 Response to "How To Scrape Data From A Google Maps"
Post a Comment