0
<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>

I want to get

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

I am trying to use the following code using beautiful 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

All i want is to get the desired output in a list but there seems to be some problem. I am not getting any print. What can be the problem?

Thank you!

Joanne
  • 67
  • 1
  • 9

1 Answers1

1

The first problem is that you are looking for span elements with seo_itemscope class which don't exist. Use seo_itemprop-title if you are looking for the titles:

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

The other problem is here:

Area=' , '.join(Area)

You meant to join items of the Type list instead:

Area = ' , '.join(Type)

And, it is not a good idea to catch the StandardError - it is too broad of an exception and actually is close to having a bare except clause. You should catch more specific exceptions, see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thanks. What if i want to get only the fourth element in the list? i.e. Apartment/Flat for Rent and ignore the previous three? python allows it? – Joanne Jun 29 '16 at 16:17
  • @Joanne sure, do you mean `soup.find_all("span", {"class": "seo_itemprop-title"})[3].get_text()`? – alecxe Jun 29 '16 at 16:25
  • @Joanne hey, Joanne - is there anything else I can help with in this particular topic? If not, please consider marking the answer as accepted to resolve the thread. Thanks. – alecxe Jun 29 '16 at 17:16