python - bs 里 find() 问题

 华华eva3 发布于 2022-10-27 18:27

源码:

# coding: UTF-8
from bs4 import BeautifulSoup
html_doc = """
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

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

输出:

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

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

3 个回答
  • a是p的子标签,会一起全部打印出来的。

    2022-10-29 01:45 回答
  • soup.find("a", attrs={"class": "sister"})
    
    或
    
    soup.find("p", class_="sister")

    可以参考 https://www.crummy.com/softwa...

    2022-10-29 01:48 回答
  • 这个是正常的,<a>是<p>标签的子元素。

    2022-10-29 01:48 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有