2017-02-06 26 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

相關問題