单击提交按钮后使用Python获取新的URL

时间:2019-02-10 22:25:43

标签: python web-scraping get geolocation gps

我正在使用Google Map获取我正在搜索的地址的GPS坐标。我单击提交后想要获取URL,以便可以从URL中提取GPS坐标。但是我的网址仅显示:https://www.google.com/maps

        url = "http://maps.google.com/"
        locationAdrs = '957 ASHBY GROVE SW ATLANTA' 
        browser = webdriver.Chrome(executable_path="C:/Users/joe/AppData/Local/Programs/Python/Python37-32/PyOn/chromedriver")
        browser.get(url)
        address = browser.find_element_by_xpath('//*[@id="searchboxinput"]')
        address.send_keys(locationAdrs)
        address.submit()
        url = browser.current_url
        print(url) 

1 个答案:

答案 0 :(得分:1)

您必须重新确认实际访问的链接是什么,因为您输入的链接可能与最终将您连接到最终目的地的DNS路由不对应。然后,您必须等待浏览器更新,然后再返回要访问的新地址:

url = "https://www.google.com/maps"
locationAdrs = '957 ASHBY GROVE SW ATLANTA'
browser.get(url)
address = browser.find_element_by_xpath('//*[@id="searchboxinput"]')
address.send_keys(locationAdrs)
# address.submit() - doesn't seem to do the right thing.
url = browser.current_url # have initial url on same format before click is made to move away
browser.find_element_by_xpath('//*[@id="searchbox-searchbutton"]').click()
while url == browser.current_url:
    time.sleep(2)
url = browser.current_url
print(url)

输出: https://www.google.com/maps/place/957+Ashby+Grove+SW,+Atlanta,+GA+30314,+USA/@33.7500669,-84.4211224,17z/data=!3m1!4b1!4m5!3m4!1s0x88f5035d3de5336f:0x9ca82913b5ecbde!8m2!3d33.7500669!4d-84.4189284