我在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
答案 0 :(得分:3)
问题在于eachproperty['href'] is None
或eachproperty.string is None
。
在尝试将它们连接在一起(即+它们)之前,您应该测试这些变量是否为None。
试
print eachproperty['href'], eachproperty.string
如果你只想打印出来,你会看到一个是无。