使用BeautifulSoup提取没有标签的文本

时间:2014-04-30 05:15:59

标签: python web-scraping beautifulsoup

我的网页就是这样 -

<p>
    <strong class="offender">YOB:</strong> 1987<br />
    <strong class="offender">RACE:</strong> WHITE<br />
    <strong class="offender">GENDER:</strong> FEMALE<br />
    <strong class="offender">HEIGHT:</strong> 5'05''<br />
    <strong class="offender">WEIGHT:</strong> 118<br />
    <strong class="offender">EYE COLOR:</strong> GREEN<br />
    <strong class="offender">HAIR COLOR:</strong> BROWN<br />
</p>

我想为每个人提取信息并获取YOB:1987RACE:WHITE等....

我尝试的是 -

subc = soup.findAll('p')
subc1 = subc[1]
subc2 = subc1.findAll('strong')

但这只给我YOB:RACE:

的值

我是否有办法以YOB:1987RACE:WHITE格式获取数据?

4 个答案:

答案 0 :(得分:52)

只需遍历所有<strong>代码,然后使用next_sibling即可获得所需内容。像这样:

for strong_tag in soup.find_all('strong'):
    print strong_tag.text, strong_tag.next_sibling

<强>演示:

>>> from bs4 import BeautifulSoup
>>> html = '''
... <p>
...     <strong class="offender">YOB:</strong> 1987<br />
...     <strong class="offender">RACE:</strong> WHITE<br />
...     <strong class="offender">GENDER:</strong> FEMALE<br />
...     <strong class="offender">HEIGHT:</strong> 5'05''<br />
...     <strong class="offender">WEIGHT:</strong> 118<br />
...     <strong class="offender">EYE COLOR:</strong> GREEN<br />
...     <strong class="offender">HAIR COLOR:</strong> BROWN<br />
... </p>
... '''
>>> soup = BeautifulSoup(html)
>>> for strong_tag in soup.find_all('strong'):
...     print strong_tag.text, strong_tag.next_sibling

这会给你:

YOB:  1987
RACE:  WHITE
GENDER:  FEMALE
HEIGHT:  5'05''
WEIGHT:  118
EYE COLOR:  GREEN
HAIR COLOR:  BROWN

答案 1 :(得分:23)

我认为你可以使用subc1.text来获取它。

>>> html = """
<p>
    <strong class="offender">YOB:</strong> 1987<br />
    <strong class="offender">RACE:</strong> WHITE<br />
    <strong class="offender">GENDER:</strong> FEMALE<br />
    <strong class="offender">HEIGHT:</strong> 5'05''<br />
    <strong class="offender">WEIGHT:</strong> 118<br />
    <strong class="offender">EYE COLOR:</strong> GREEN<br />
    <strong class="offender">HAIR COLOR:</strong> BROWN<br />
</p>
"""
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> print soup.text


YOB: 1987
RACE: WHITE
GENDER: FEMALE
HEIGHT: 5'05''
WEIGHT: 118
EYE COLOR: GREEN
HAIR COLOR: BROWN

或者如果你想探索它,你可以使用.contents

>>> p = soup.find('p')
>>> from pprint import pprint
>>> pprint(p.contents)
[u'\n',
 <strong class="offender">YOB:</strong>,
 u' 1987',
 <br/>,
 u'\n',
 <strong class="offender">RACE:</strong>,
 u' WHITE',
 <br/>,
 u'\n',
 <strong class="offender">GENDER:</strong>,
 u' FEMALE',
 <br/>,
 u'\n',
 <strong class="offender">HEIGHT:</strong>,
 u" 5'05''",
 <br/>,
 u'\n',
 <strong class="offender">WEIGHT:</strong>,
 u' 118',
 <br/>,
 u'\n',
 <strong class="offender">EYE COLOR:</strong>,
 u' GREEN',
 <br/>,
 u'\n',
 <strong class="offender">HAIR COLOR:</strong>,
 u' BROWN',
 <br/>,
 u'\n']

并从列表中过滤掉必要的项目:

>>> data = dict(zip([x.text for x in p.contents[1::4]], [x.strip() for x in p.contents[2::4]]))
>>> pprint(data)
{u'EYE COLOR:': u'GREEN',
 u'GENDER:': u'FEMALE',
 u'HAIR COLOR:': u'BROWN',
 u'HEIGHT:': u"5'05''",
 u'RACE:': u'WHITE',
 u'WEIGHT:': u'118',
 u'YOB:': u'1987'}

答案 2 :(得分:0)

您可以尝试使用此内部findall for循环:

item_price = item.find('span', attrs={'class':'s-item__price'}).text

它仅提取文本并将其关联到“ item_pice”

答案 3 :(得分:0)

我认为您可以使用gazpacho中的.strip()解决此问题:

输入:

html = """\
<p>
  <strong class="offender">YOB:</strong> 1987<br />
  <strong class="offender">RACE:</strong> WHITE<br />
  <strong class="offender">GENDER:</strong> FEMALE<br />
  <strong class="offender">HEIGHT:</strong> 5'05''<br />
  <strong class="offender">WEIGHT:</strong> 118<br />
  <strong class="offender">EYE COLOR:</strong> GREEN<br />
  <strong class="offender">HAIR COLOR:</strong> BROWN<br />
</p>
"""

代码:

soup = Soup(html)
text = soup.find("p").strip(whitespace=False) # to keep \n characters intact
lines = [
    line.strip()
    for line in text.split("\n")
    if line != ""
]
data = dict([line.split(": ") for line in lines])

输出:

print(data)
# {'YOB': '1987',
#  'RACE': 'WHITE',
#  'GENDER': 'FEMALE',
#  'HEIGHT': "5'05''",
#  'WEIGHT': '118',
#  'EYE COLOR': 'GREEN',
#  'HAIR COLOR': 'BROWN'}