BeautifulSoup 如何解析带特殊格式的h1的文本?

新手上路,请多包涵

问题描述

有一个html中含有此片段:
<h1>蜘蛛侠:英雄远征 Spider-Man: Far from Home (2019)

            <small class="label label-success">资源数  <b>( 60 )</b></small> </h1>

我想把蜘蛛侠:英雄远征 Spider-Man: Far from Home (2019)给提取出来

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
我尝试过film_name = soup.h1

    name = film_name.string
    其中soup是整个页面的html

会出错

你期待的结果是什么?实际看到的错误信息又是什么?

总是提示:

Traceback (most recent call last):
  File "main.py", line 68, in <module>
    get_more_film('http://pianyuan.la/m_S8oNccec0.html')
  File "main.py", line 62, in get_more_film
    number = number_div.find('b')
  File "C:\Program Files\Python37\lib\site-packages\bs4\element.py", line 1578, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
阅读 3.7k
1 个回答

如果你是单纯想要提取片名 可以把small这个标签给去除掉

代码如下

[soup.find('h1').find_all('small')[0].extract()]

刚刚看了这个页面 你可以直接提取页面的title标签

<title>蜘蛛侠:英雄远征 Spider-Man: Far from Home (2019) 下载</title>

并且用进行字符串切片去掉“下载”字样

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题