0

我在 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
4

1 回答 1

3

问题是,eachproperty['href'] is None要么eachproperty.string is None

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

尝试

print eachproperty['href'], eachproperty.string

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

于 2013-09-04T07:25:40.570 回答