2017-02-06 20 views
1

我正在尝试在Google地图中获取section-facts-description-text如何在Google Maps中使用python获取section-facts-description-text?

我曾经尝试这样的代码已经:

import urllib 
from bs4 import BeautifulSoup 

url = "https://www.google.co.id/maps/place/Semarang,+Kota+Semarang,+Jawa+Tengah/@-7.0247703,110.3488077,12z/data=!3m1!4b1!4m5!3m4!1s0x2e708b4d3f0d024d:0x1e0432b9da5cb9f2!8m2!3d-7.0051453!4d110.4381254" 
html = urllib.urlopen(url).read() 
soup = BeautifulSoup(html,"html.parser") 

# kill all script and style elements 
for script in soup(["script", "style"]): 
    script.extract() # rip it out 

# get text 
text = soup.get_text() 

for strong_tag in soup.find_all('span',{'class':'section-facts-description-text'}): 
    print strong_tag.text, strong_tag.next_sibling 

这有什么错我的代码?有什么我失踪?是否有任何选项可以在python中使用库或API来执行该操作?

回答

1

urllib请求初始加载数据关闭网页,然后终止。在包含Google地图的许多复杂的非静态网页的情况下,有效载荷几乎全部由JavaScript脚本组成,然后然后按照您所知的那样填充页面。

因此,不是下载所需的DOM元素等,而是下载填充所有内容的JavaScript代替。

为了下拉加载的GMaps页面,您需要使用能够打开页面的网络驱动程序,等待加载,然后只有然后下载内容。为此,您应该调查selenium

相关问题