【发布时间】:2016-10-05 19:47:25
【问题描述】:
这个问题有点蹩脚:我在这个网站页面上使用Beautiful soup, url = 'http://www.rentatbelleparkeast.com/Apartments/module/property_info/property%5Bid%5D/13578/' 来提取以下信息。 1.名称 2. 地址 3. 电话 4. 公寓价格(租金、布局-床/浴室/尺寸。 我有以下简单的代码,但我没有正确的锚标签来提取实际信息。我在搜索结果中变得空白(无)。
import urllib
from BeautifulSoup import *
url = 'http://www.rentatbelleparkeast.com/Apartments/module/property_info/property%5Bid%5D/13578/'
html = urllib.urlopen(url).read()
#Create beautiful soup object
soup = BeautifulSoup(html)
print soup
# Retrieve a list of the anchor tags
# Each tag is like a dictionary of HTML attributes
#For Phone I use tag 'span'
tag = soup('span')
#I search for Phone tag but this gives blank output
phone = soup.find(text= 'Phone: ')
# I use tag 'td' to get all information from the tables
tab = soup.findAll('td')
print tab # This gives the object with all information I need
#I extract, for example, Rent using the following loop. I get blank (None) results
for i in tab:
print i.get('Rent *', None)
【问题讨论】:
-
您正在使用
BeautifulSoup版本 3,它非常非常非常旧。请升级到bs4:pip install beautifulsoup4并将您的导入更改为from bs4 import BeautifulSoup。
标签: python-2.7