[英]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"}
對於以后出現的內容,如果您:
這將打開OP尋找的地圖。
現在解決了... A
標簽看起來像
<a target="_blank"
href="https://maps.google.com/maps?ll=51.552574,-0.052437&z=15&t=m&hl=en-US&gl=US&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選擇器,因為它們更快且不易出錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.