簡體   English   中英

Python Web抓取-如何找到map元素的坐標?

[英]Python Web scraping - How to locate the Co-ordinates of the map element?

我正在做一些網頁抓取工作,並在python中使用硒。 我無法找到地圖元素,也無法從中獲取經度和緯度。 誰能看一下代碼。 這是我的代碼,

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://www.zoopla.co.uk/to-rent/commercial/details/41716558?search_identifier=c2df4fb926a1dd55be2a0e31c706380b#1cy1zTwJA1aKrxcV.97")
maps = driver.find_element_by_xpath("//*[@id='listing-details-tabs']/ul/li[2]")
maps.click()

laltong=driver.find_element_by_xpath("//*[@id='bto-rent-commercial']/script[6]/text()")
print(latlong.text)

我知道我用於定位元素的xpath完全錯誤,因為我什至無法定位該元素。 有什么建議嗎? 這是網頁的鏈接-http://www.zoopla.co.uk/to-rent/commercial/details/42080041?search_identifier = f02093ba0369b1204ac8cf6369e419f5# RuZu0HmxXSL3auBJ.97 ,然后點擊地圖和附近。

我嘗試過的最新代碼是

laltong=driver.find_element_by_xpath("//*[@title='Click to see this area on Google Maps']/div/div[1]/a")
print(latlong.get_attribute("href"))

錯誤是

Unable to locate element: {"method":"xpath","selector":"//*[@title='Click to see this area on Google Maps']/div/div[1]/a"}

對於以后出現的內容,如果您:

  1. 導航到此鏈接
  2. 點擊地圖和附近的標簽
  3. 點擊地圖左下角的Google徽標(淺色背景上的白色文字...很難看到)

這將打開OP尋找的地圖。

現在解決了... A標簽看起來像

<a target="_blank"
  href="https://maps.google.com/maps?ll=51.552574,-0.052437&amp;z=15&amp;t=m&amp;hl=en-US&amp;gl=US&amp;mapclient=apiv3"
  title="Click to see this area on Google Maps"
  style="position: static; overflow: visible; float: none; display: inline;">...</a>

href屬性是我們想要的重要部分。

要獲得此A標記,我們可以使用CSS選擇器"a[title='Click to see this area on Google Maps']"來查找元素。 從那里您將獲得href屬性,我假設您已經知道該怎么做。


更新后,我看到您正在使用XPath。 您的XPath已關閉,但是包含title="Click to see this area on Google Maps"的元素就是您想要的A ,因此XPath為"//a[@title='Click to see this area on Google Maps']" 我建議您使用CSS選擇器,因為它們更快且不易出錯。

CSS選擇器參考

CSS選擇器提示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM