使用python

时间:2016-06-29 15:12:24

标签: python web-scraping beautifulsoup findall bs4

<div id="browse_in_widget">
<span id="browse_in_breadcrumb" style="width: 583px;">
<div class="seo_itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="">
<a itemprop="url" href="/search/"> Arabian Area</a>
<span class="seo_itemprop-title" itemprop="title">Arabian Area</span>
>
</div>
<div class="seo_itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="">
<a itemprop="url" href="/property-for-rent/home/"> Phase 2 </a>
<span class="seo_itemprop-title" itemprop="title">Phase 2 </span>
>
</div>
<div class="seo_itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="">
<a itemprop="url" href="/property-for-rent/residential/"> Residential Units for Rent </a>
<span class="seo_itemprop-title" itemprop="title">Residential Units for Rent</span>
>
</div>
<div class="seo_itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="">
<a itemprop="url" href="/property-for-rent/residential/apartmentflat/"> Apartment/Flat for Rent </a>
<span class="seo_itemprop-title" itemprop="title">Apartment/Flat for Rent</span>
>
</div>
<strong class="seo_itemprop-title" itemprop="title">Details</strong>
</span>
</div>

我想要

['Arabian Area', 'Phase 2', 'Residential Units for Rent','Apartment/Flat for Rent']

我正在尝试使用以下代码使用漂亮的4 python

try:

        Type = [str(Area.text) for Area in soup.find_all("span", {"class" : "seo_itemscope"})]

        Area=' , '.join(Area)
        print Area


    except StandardError as e:
        Area="Error was {0}".format(e)
        print Area

我想要的只是在列表中获得所需的输出,但似乎有一些问题。我没有得到任何印刷品。可能是什么问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

第一个问题是您正在查找span类的seo_itemscope元素,这些元素不存在。如果您正在寻找标题,请使用seo_itemprop-title

Type = [item.get_text() for item in soup.find_all("span", {"class": "seo_itemprop-title"})]

另一个问题在于:

  

Area=' , '.join(Area)

您的意思是加入Type列表的项目:

Area = ' , '.join(Type)

并且,抓住StandardError并不是一个好主意 - 它是一个过于宽泛的异常,实际上接近于只有一个except子句。您应该捕获更具体的例外情况,请参阅: