简体   繁体   中英

TypeError: cannot concatenate 'str' and 'NoneType' objects python bs4

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

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 (ie + them).

try

print eachproperty['href'], eachproperty.string

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM