我正在尝试使用python从Google地图中抓取标题,电话号码,网站,地址,等级,评论的数量。例如,餐厅Pike's Landing(请参阅下面的Google Maps URL)需要所有信息。我想将它们拉入python。
URL:https://www.google.com/maps?cid=15423079754231040967&hl=en
检查时可以看到HTML代码,但是当我使用漂亮的汤来报废时,所有代码都将转换。从堆栈溢出中,我找到了一个唯一的解决方案,如下代码
import re
import requests
from ast import literal_eval
urls = [
'https://www.google.com/maps?cid=15423079754231040967&hl=en',
'https://www.google.com/maps?cid=16168151796978303235&hl=en']
for url in urls:
for g in re.findall(r'\[\\"http.*?\d+ reviews?.*?]', requests.get(url).text):
data = literal_eval(g.replace('null', 'None').replace('\\"', '"'))
print(bytes(data[0], 'utf-8').decode('unicode_escape'))
print(data[1])
但是我需要所有数据。我可以使用Google Maps API来获取实际数据,但是获取电话号码,评分,评论现在不是免费的。这样我想从前端转义数据。
请帮助我。
答案 0 :(得分:2)
如果您检查页面源代码,您会发现 match_history_data = [{"DateCollected" : item["dateCollected"], "Orders" : item["orders"]} for item in dataset]
块,其中包含带有所有地点数据的 JSON。你只需要解析它。
您也可以使用第三方解决方案,例如 SerpApi。这是一个免费试用的付费 API。
window.APP_INITIALIZATION_STATE
示例输出:
from serpapi import GoogleSearch
params = {
"engine": "google_maps",
"type": "place",
"q": "Pike's Landing",
"ll": "@40.7455096,-74.0083012,14z",
"data": "!3m1!4b1!4m5!3m4!1s0x0:0xd609c9524d75cbc7!8m2!3d64.8299557!4d-147.8488774"
"api_key": "API_KEY",
}
search = GoogleSearch(params)
results = search.get_dict()
您可以查看documentation了解更多详情。
免责声明:我在 SerpApi 工作。
答案 1 :(得分:1)
很久以前,我在reddit上问了同样的问题。我最终自己解决了这个问题,have a look at this注意-严格地编写此代码是为了提取我的用例的详细信息,但是您可以大致了解这里的情况。
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
browser = webdriver.Chrome(options=options)
url = "https://www.google.com/maps/place/Papa+John's+Pizza/@40.7936551,-74.0124687,17z/data=!3m1!4b1!4m5!3m4!1s0x89c2580eaa74451b:0x15d743e4f841e5ed!8m2!3d40.7936551!4d-74.0124687"
# url = "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"
browser.get(url)
# review titles / username / Person who reviews
review_titles = browser.find_elements_by_class_name("section-review-title")
print([a.text for a in review_titles])
# review text / what did they think
review_text = browser.find_elements_by_class_name("section-review-review-content")
print([a.text for a in review_text])
# get the number of stars
stars = browser.find_elements_by_class_name("section-review-stars")
first_review_stars = stars[0]
active_stars = first_review_stars.find_elements_by_class_name("section-review-star-active")
print(f"the stars the first review got was {len(active_stars)}")