我是靠谱客的博主 纯情钥匙,这篇文章主要介绍使用 selenium 爬取谷歌地图的经纬度,现在分享给大家,希望可以做个参考。

转载于:博主【一口气吃五碗饭的阿霖】
万分感谢

思路 :
谷歌搜索地名的时候经纬度会出现在url中,所以我们可以利用这个来获取经纬度
由于谷歌在搜索地址的时候有可能因为网络原因 url依旧显示的是你电脑所在的地址
所以必须要先获取你电脑所在的地址。方便后面进行比对。

code:

复制代码
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
# 导入需要的包 from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait import time import sys import re import os import csv import numpy as np # print(sys.path) # help(webdriver) def getLandL(add): L1Mark, L2Mark = .0, .0 # 设置两个变量 装载自己电脑所在的经纬度 for no in range(10): options = Options() options.add_argument('--headless') # 不打开浏览器 options.add_argument('--disable-gpu') # 禁用GPU硬件加速 options.add_argument( 'user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1"') # 添加访问头 # options.add_argument('proxy-server="60.13.42.109:9999"') # 添加代理 driver = webdriver.Chrome(options=options) # 使用驱动配置 driver.set_window_size(1920, 1080) # 先獲取电脑本身的經緯度 以防出錯 driver.get("https://www.google.com.hk/maps/place/") time.sleep(8) # 等待时间 python_button = driver.find_elements_by_xpath("//*[@id="searchbox-searchbutton"]")[0] python_button.click() time.sleep(8) # 等待时间 getUrl = driver.current_url # 獲取需要爬取的地址 element = re.findall("https://www.google.com.hk/maps/@(d+.d+).*?(d+.d+)", getUrl, re.S) if element != []: L1Mark, L2Mark = float(element[0][0]), float(element[0][1]) driver.get("https://www.google.com.hk/maps/place/" + add) driver.implicitly_wait(15) # 等待时间 for timerOut in range(5): python_button = driver.find_elements_by_xpath("//*[@id="searchbox-searchbutton"]")[0] python_button.click() time.sleep(8) getUrl = driver.current_url element = re.findall("https://www.google.com.hk/maps/.*?/.*?@(d+.d+).*?(d+.d+)", getUrl, re.S) if element != []: L1End, L2End = float(element[0][0]), float(element[0][1]) L1, L2 = float(element[0][0]), float(element[0][1]) if ((L1Mark != L1 and L2Mark != L2) or (no >= 7 and timerOut >= 3)): driver.close() return L1, L2 driver.close() time.sleep(20) print(L1End, L2End) return [L1End, L2End] # 调用只需要把你想要的爬的address写入就可以了 print(getLandL("shanghai")) #(31.2240453, 121.1965748)

最后

以上就是纯情钥匙最近收集整理的关于使用 selenium 爬取谷歌地图的经纬度的全部内容,更多相关使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部