登录

s 里 find() 问题

源码:

# coding: UTF-8
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
data = soup.find('p', {'class': 'story'})
print(data)

输出:

<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

问题:我想爬取的是 p 标签下的 属性值为 story 的 class, 但是结果怎么出来有 a 标签呢?

# Python
高洛峰 高洛峰 2732 天前 757 次浏览

全部回复(1) 我要回复

  • 三叔

    三叔2016-10-22 16:08:45

    这个是正常的,

    标签的子元素。

    回复
    0
  • 取消 回复 发送