I am learning Python and hope to get some information from a map navigation container of this website https://findmasa.com/view/map#b1cc410b, such as mural id, latitude, longitude, artist name, address, city, and state.
Below is the code I tried before, but the output is always NO DATA. My coding skill is limited so any help would be sincerely appreciated!
from bs4 import BeautifulSoup
import requests
url = 'https://findmasa.com/view/map#b1cc410b'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
li_element = soup.find('li', id='b1cc410b')
if li_element:
data_lat = li_element['data-lat']
data_lng = li_element['data-lng']
artist_name = li_element.find('a').text
address = li_element.find_all('p')[1].text
city = li_element.find_all('p')[2].text
print('LATITUDE ', data_lat)
print('LONGITUDE ', data_lng)
print('ARTIST ', artist_name)
print('ADDRESS ', address)
print('CITY ', city)
else:
print('NO DATA')
The information you're looking for gets loaded slowly and involves Javascript. As the
requests
library doesn't support the javascript, it doesn't return the content/information and thus yourif-statement
getsFalse
. So, it goes to theelse-statement
and you getNO DATA
.You may try using
Selenium
Here's the solution
output:
You can install selenium using pip:
[UPDATE]:
f-string
InvalidSelectorException
that you're getting for some url or better to say for some id number, use the notationli[id="id_value"]
instead ofli#id_value
. output: