TypeError:无法连接'str'和'NoneType'对象python bs4

时间:2013-09-04 07:22:26

标签: python web-scraping beautifulsoup

我在python中编写了一个简单的程序来进行抓取。我对此很新。我无法理解bs4文档中提供的内容

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

我收到以下错误

    /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 个答案:

答案 0 :(得分:3)

问题在于eachproperty['href'] is Noneeachproperty.string is None

在尝试将它们连接在一起(即+它们)之前,您应该测试这些变量是否为None。

print eachproperty['href'], eachproperty.string

如果你只想打印出来,你会看到一个是无。