Search code examples
pythonparsingbeautifulsouphtml-parsing

Can't parse longitude and latitude BeautifulSoup


If i use this

 'latitude': item.find('div', class_='data-shop-latitude').get_text(),
 'longitude': item.find('div', class_='data-shop-longitude').get_text(),

I get

AttributeError: 'NoneType' object has no attribute 'get_text'

If i use that

'latitude': item.find('div', class_='data-shop-latitude'),
'longitude': item.find('div', class_='data-shop-longitude'),

I get

'latitude': None, 'longitude': None,

How i can get this:

'latitude': 52.42065, 'longitude': 37.59659,


Solution

  • find takes a kwarg called attrs that you should use.

    Once you've got the shops with soup.find_all('div', attrs={'class':'shop-list-item'}), all you need to do is get the value of the different attributes you're interested in using the get method on the div element (whose type is bs4.element.Tag:

    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get(r'https://www.mebelshara.ru/contacts')
    
    soup = BeautifulSoup(r.text, 'html.parser')
    
    shops = soup.find_all('div', attrs={'class':'shop-list-item'})
    for shop in shops:
        print(shop.get('data-shop-name'))
        print(shop.get('data-shop-latitude'))
        print(shop.get('data-shop-longitude'))
        print()
    

    Output:

    ТЦ Европа
    50.59084
    36.59734
    
    ТЦ Атлас
    50.58516
    36.565457
    
    ТЦ РИО
    50.64208
    36.572086
    
    [...]