【问题标题】:Extract Specific info from 'span' and 'td' anchor tags with Beautifulsoup使用 Beautifulsoup 从“span”和“td”锚标签中提取特定信息
【发布时间】: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


【解决方案1】:

简单的姓名、地址和您可以使用的电话

name = ' '.join(soup.find('div', {'class': 'limitedTitle'}).text.split())
adress = ' '.join(soup.find('div', {'class': 'limitedText'}).text.split())
phone = ''.join(soup.find('div', {'class': 'propertyContactText'}).text.split())

【讨论】:

  • 我想概括搜索几个网址(不同网站)的姓名、地址、电话和租金。上面的代码无法解析下一个 url 的相同信息。是否有一个标准标签(美丽的汤),我可以从 url 列表中使用这些数据
  • 每个标签/类名等,通常因站点而异。在同一个站点上,当您检查不同的子站点时,它将是相同的。但是,如果我想创建该工具,我会根据给定的属性创建用于解析文本信息的类。
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
  • 2022-11-01
  • 1970-01-01
  • 2019-09-01
相关资源
最近更新 更多