从python字符串中删除多余的字符

时间:2016-05-30 17:30:57

标签: python python-2.7 python-3.x web-scraping bs4

我要提取的信息: 名单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']

1 个答案:

答案 0 :(得分:0)

我假设soupBeautifulSoup个实例,因此soup = BeautifulSoup(html_string, "html.parser") html_stringhtml markup

试试这个:

area_list = [area.get_text().strip().encode('utf-8') for area in soup.find_all('span', {'class': 'location'})] print area_list