我试图用python从谷歌Map上抓取一个地方的评论数。例如,餐厅派克登陆(见下面的谷歌Map网址)有162条评论。我想在python中拉这个数字。
网址:https://www.google.com/maps?cid=15423079754231040967
我不是很精通HTML,但从一些基本的例子在互联网上我写了下面的代码,但我得到的是一个黑色变量后运行这段代码。如果你能让我知道我在这方面做错了什么,将不胜感激。
from urllib.request import urlopen
from bs4 import BeautifulSoup
quote_page ='https://www.google.com/maps?cid=15423079754231040967'
page = urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
price_box = soup.find_all('button',attrs={'class':'widget-pane-link'})
print(price_box.text)
3条答案
按热度按时间vxqlmq5t1#
在没有API的情况下,用纯Python很难做到这一点,下面是我最后的结果(注意,我在url的末尾添加了
&hl=en
,以获得英语结果,而不是我的语言):印刷品:
kg7wmglp2#
您需要查看页面的源代码,并使用正则表达式解析
window.APP_INITIALIZATION_STATE
变量块,在那里您会找到所有需要的数据。或者,您可以使用SerpApi的Google Maps Reviews API。
JSON输出示例:
要集成的代码:
输出量:
免责声明,我为SerpApi工作。
qojgxg4l3#
在没有浏览器或代理的情况下刮取谷歌Map会在几次成功请求后导致阻塞,因此刮取谷歌Map的主要问题是处理cookie和ReCaptcha。
这是一个很好的post,你可以看到一个在python中使用selenium实现同样目的的例子,一般的想法是启动一个浏览器,模拟用户在网站上做什么。
另一种方法是使用一些可靠的第三方服务,它会为你做所有的工作并返回结果。例如,你可以尝试Outscraper's Reviews service和一个免费的层。
免责声明,我为Outscraper工作。