# 3.解析数据
bs = BeautifulSoup(html_data, 'lxml')
sightList = bs.find_all('div', class_='sight_item_about') # 所有景点信息
# 遍历每个景点
for sight in sightList:
sightName = sight.find_all('a', class_='name')[0].string # 景区名
# 景区等级,有些景区无等级所以可能异常
try:
sightLevel = sight.find_all('span', class_='level')[0].string
except:
sightLevel = '无'
sightAddress = sight.find_all('p', class_='address color999')[0].span.string[3:] # 地址,去掉'地址'二字
sightDesc = sight.find_all('div', class_='intro color999')[0].string # 景区介绍
# 获取景区最低价格
try:
sightPrice = sight.find_all('span',class_='sight_item_price')
print(sightPrice)
except:
sightPrice = '免费'
# 获取月销量
try:
sightNum = sight.find_all('span', class_='hot_num')[0].string
except:
sightNum = 0
# 获取景区热度
sightStarLevel = sight.find_all('span', class_='product_star_level')[0].text[2:]
# 打印结果
print('{0},{1},{2},{3},{4},{5}'.format(sightName,sightAddress,sightDesc,sightNum,sightPrice,sightLevel))
可以看到,在代码中使用 find_all
查找的这段代码无法工作,只返回了空的列表
# 获取景区最低价格
try:
sightPrice = sight.find_all('span',class_='sight_item_price')
print(sightPrice)
except:
sightPrice = '免费'