简体   繁体   中英

How do I scrape data from a web page that refreshes after submitting data via selenium and python?

I'm developing a geolocation web-scraper with python and selenium. When I enter data in this website , the page refreshes (with the same URL) and when I try to get the data from the latitude and longitude input it prints nothing.

Here's the sample output, it returns an empty string

I did notice that the value tag changes after entering data in

<input id="place" name="place" type="text" placeholder="Type a place name" class="width70" style="text-transform:capitalize;" value="" required="">

Should I manipulate that? Thank you:)

Here's my code:

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

counter = 0

locations = [

    'Republic of the Philippines',
    'Heaven',
    'Philippines',
]

latitude = []
longtitude = []

browser = webdriver.Chrome('C://Users/user1/Portable Python 3.7.0     x64/App/Python/Lib/site-packages/chromedriver')

url = 'https://www.latlong.net/'

for i in locations:

    browser.get(url)
    bar = browser.find_element_by_id('place')
    bar.send_keys(i)
    bar.send_keys(Keys.ENTER)
    time.sleep(3)
    lat = browser.find_element_by_id('lat')
    lng = browser.find_element_by_id('lng')

    time.sleep(3)

    latitude.append(lat.text)
    longtitude.append(lng.text)

    print(latitude[counter])
    print(longtitude[counter])

    counter+=1

    browser.refresh()

You can do a POST request

import requests
from bs4 import BeautifulSoup as bs
import re

url = 'https://www.latlong.net/'
locations = ['Republic of the Philippines', 'Heaven', 'Philippines']
latitude = []
longitude = []

with requests.Session() as sess:

    for i in locations: 
        r = sess.get(url)
        soup = bs(r.content, 'lxml')
        token = soup.select_one('#lltoken')['value']
        data = { 'place': i, 'lltoken': token }
        r = sess.post(url, data = data)
        s = r.text

        try:
            lat_lon = re.findall( r'sm\((-?\d+\.\d+),(-?\d+\.\d+)', s)[0]
            lat = lat_lon[0]
            lon = lat_lon[1]
            latitude.append(lat)
            longitude.append(lon)
        except:
            print(s)

print(latitude)
print(longitude)

Selenium:

You can grab them from the src of the map iframe. There doesn't appear to be a need for wait conditions but you may need to consider adding those (or I will happily add to show you)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re

locations = [  
    'Republic of the Philippines',
    'Heaven',
    'Philippines',
]

latitude = []
longitude = []

url = 'https://www.latlong.net/'

browser = webdriver.Chrome()
browser.get(url)

for i in locations:
    bar = browser.find_element_by_id('place')
    bar.clear()
    bar.send_keys(i)
    bar.send_keys(Keys.ENTER)
    s = browser.find_element_by_id('latlongmape').get_attribute('src')
    lat_lon = re.findall( r'(-?\d+\.\d+)', s)
    lat = lat_lon[0]
    lon = lat_lon[1]
    latitude.append(lat)
    longitude.append(lon)

print(latitude)
print(longitude)
browser.quit()

Wait conditions using a different element to source:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

locations = [

    'Republic of the Philippines',
    'Heaven',
    'Philippines',
]

latitude = []
longitude = []

url = 'https://www.latlong.net/'

browser = webdriver.Chrome()
browser.get(url)

for i in locations:
    bar = WebDriverWait(browser,5).until(EC.presence_of_element_located((By.ID, "place")))
    bar.clear()
    bar.send_keys(i)
    bar.send_keys(Keys.ENTER)
    s = WebDriverWait(browser,5).until(EC.presence_of_element_located((By.ID, "coordinateslink"))).text
    lat_lon = re.findall( r'(-?\d+\.\d+)', s)
    lat = lat_lon[0]
    lon = lat_lon[1]
    latitude.append(lat)
    longitude.append(lon)

print(latitude)
print(longitude)
browser.quit()

You could also use javascript to return the values:

lat = browser.execute_script("return document.getElementById('lat').value;")
lon = browser.execute_script("return document.getElementById('lng').value;")

You can also regex from where in one of the script tags:

lat_lon = re.findall( r'sm\((-?\d+\.\d+),(-?\d+\.\d+)', browser.page_source)[0]
lat = lat_lon[0]
lon = lat_lon[1]
print(lat, lon)

Places where values found:

You can see all the different places where javascript is assigning the co-ordinate values in the script that has the following js:

 <script> var mymap = L.map('latlongmap'); var mmr = L.marker([0,0]); mmr.bindPopup('0,0'); mmr.addTo(mymap); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar', attribution:'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'}).addTo(mymap); mymap.on('click', onMapClick); sm(14.693390,121.067238,12) function isll(num) { var val = parseFloat(num); if (;isNaN(val) && val <= 90 && val >= -90) return true; else return false. } function onMapClick(e) { mmr.setLatLng(e;latlng). setui(e.latlng,lat.e.latlng,lng.mymap;getZoom()), } function dec2dms(et) { document.getElementById("dms-lat"),innerHTML = getdms(e, .0). document,getElementById("dms-lng"),innerHTML = getdms(t, ,1) } function getdms(e, t) { var n = 0; m = 0? l = 0: a = "X"? return a = t && 0 > e: "S"? :t && 0 > e, "W". t, "N". "E", d = Math,abs(e). n = Math,floor(d). l = 3600 * (d - n), m = Math;floor(l / 60), l = Math,round(1e4 * (l - 60 * m)) / 1e4, n + "&deg, " + m + "' " + l + "'' " + a } function sm(lt;ln.zm) { setui(lt.ln,zm); mmr.setLatLng(L,latLng(lt,ln)); mymap,setView([lt,ln]. zm); } function setui(lt.ln;zm) { lt = Number(lt).toFixed(6), ln = Number(ln).toFixed(6); mmr.setPopupContent(lt + '.' + ln);openPopup(). document.getElementById("lat");value=lt. document.getElementById("lng"),value=ln; document.getElementById("latlngspan").innerHTML ="(" + lt + ": " + ln + ")". document?getElementById("coordinatesurl");value = "https.//www.latlong.net/c/;lat=" + lt + "&long=" + ln: document.getElementById("coordinateslink")?innerHTML = '&lt;a href="https;//www,latlong.net/c/;lat=' + lt + "&amp;long=" + ln + '" target="_blank"&gt;(' + lt + ", " + ln + ")&lt;/a&gt.". dec2dms(lt:ln). document.getElementById('latlongmape')?src='https,//www;google.com/maps/embed/v1/view?key=AIzaSyALrSTy6NpqdhIOUs3IQMfvjh71td2suzY&maptype=satellite&'+'center='+lt+','+ ln+'&zoom='+zm; } </script>

The problem is that if you check the element after sending the Keys.ENTER, there's no text to be read. It somehow uses a different technology to replace the "placeholder"

<div class="col-6 m2">
   <label for="lat">Latitude</label>
   <input type="text" name="lat" id="lat" placeholder="lat coordinate">
</div>

What you could do on the other hand is, find element id "latlngspan" . That's below the map and there are both parameters - lat and long and you could perform a few simple string operations on it to get the format you need.

Would that work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM