0

I wrote a simple program in python to do scraping. I am very new to this. I just cannot understand thing that are provided in the bs4 documentation

from bs4 import BeautifulSoup
import urllib2
url="http://www.99acres.com/property-in-velachery-chennai-south-ffid?"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
properties=soup.findAll('a',{'class':'f15'})
for eachproperty in properties:
 print eachproperty['href']+","+eachproperty.string

I get the following error

    /Residential-Apartment-Flat-in-Velachery-Chennai South-2-Bedroom-bhk-for-Sale-spid-Y10765227,2 Bedroom, Residential Apartment in Velachery
Traceback (most recent call last):
  File "properties.py", line 8, in <module>
    print eachproperty['href']+","+eachproperty.string
TypeError: cannot concatenate 'str' and 'NoneType' objects

1 Answer 1

3

The problem is that either eachproperty['href'] is None or eachproperty.string is None.

You should test to see if these variables are None before you try to concatenate them together (i.e. + them).

try

print eachproperty['href'], eachproperty.string

if you just want to print them out, you will see that one is None.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. It worked but please look at the following output. /Residential-Apartment-Flat-in-Velachery-Chennai South-2-Bedroom-bhk-for-Sale-spid-Y10765227 2 Bedroom, Residential Apartment in Velachery javascript:void(0); None javascript:void(0); View Phone Number /Residential-Apartment-Flat-in-Velachery-Chennai South-1-Bedroom-bhk-for-Sale-spid-L10038779 1 Bedroom, Residential Apartment in Velachery javascript:void(0); None and so on..
hard to believe python can't just handle this ... java's toString()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.