我要提取的信息:
名单Al Bayan和尼泊尔名单['Al Bayan' , 'Nepal']
<div class="location">
<div class="listing-location">Location</div>
<div class="location-areas">
<span class="location">Al Bayan</span>
,
<span class="location">Nepal</span>
</div>
<div class="area-description"> 3.3 km from Mall of the Emirates </div>
</div>
提取区域的代码:
try:
area= soup.find('div', 'location-areas')
area_result= str(area.get_text().strip().encode("utf-8"))
print([area_result])
except StandardError as e:
area_result="Error was {0}".format(e)
print area_result
输出:
"Al Bayanأ¢â‚¬آھ,أ¢â‚¬آھ
Nepal"
期望输出:
['Al Bayan', 'Nepal']
答案 0 :(得分:0)
我假设soup
是BeautifulSoup
个实例,因此soup = BeautifulSoup(html_string, "html.parser")
html_string
是html markup
。
试试这个:
area_list = [area.get_text().strip().encode('utf-8') for area in soup.find_all('span', {'class': 'location'})]
print area_list