【问题标题】:Can't parse longitude and latitude BeautifulSoup无法解析经纬度 BeautifulSoup
【发布时间】:2020-06-28 02:42:58
【问题描述】:

如果我用这个

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

我明白了

AttributeError: 'NoneType' 对象没有属性 'get_text'

如果我用那个

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

我明白了

'纬度':无,'经度':无,

我怎么能得到这个:

“纬度”:52.42065,“经度”:37.59659,

【问题讨论】:

标签: python parsing beautifulsoup html-parsing


【解决方案1】:

find 需要一个名为 attrs 的 kwarg,您应该使用它。

一旦你有了soup.find_all('div', attrs={'class':'shop-list-item'}) 的商店,你需要做的就是使用get 方法在div 元素(其类型是@ 987654328@:

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()

输出:

ТЦ Европа
50.59084
36.59734

ТЦ Атлас
50.58516
36.565457

ТЦ РИО
50.64208
36.572086

[...]

【讨论】:

  • AttributeError: 'NoneType' 对象没有属性 'get_text'
  • 如果还是这样,这意味着在item 元素中,没有div 元素的class 属性设置为data-shop-latitudedata-shop-latitude。如果您需要更多帮助,您需要按照 Igor Hwang 的建议发布 html/url。
  • pastebin.com/eJqgkZH2 — html > items = soup.find_all('div', class_='shop-list-item') url — mebelshara.ru/contacts
  • 我已经更新了我的答案。你已经[几乎,修改了你的代码]已经得到了你想要的div元素。您只需要使用get 方法读取属性即可。
猜你喜欢
  • 2010-11-21
  • 2013-03-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多