如何使用BeautifulSoup bs4获取HTML标签的内部文本值? [英] How to get inner text value of an HTML tag with BeautifulSoup bs4?

查看:1658
本文介绍了如何使用BeautifulSoup bs4获取HTML标签的内部文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用BeautifulSoup bs4时,如何从HTML标签中获取文本?当我运行这一行时:

When using BeautifulSoup bs4, how to get text from inside a HTML tag? When I run this line:

oname = soup.find("title")

我得到标题标签,如下所示:

I get the title tag like this:

<title>page name</title>

现在我只想得到它的内部文本,页面名称,没有标签。如何做到这一点?

and now I want to get only the inner text of it, page name, without tags. How to do that?

推荐答案

使用.text从标签中获取文本。

Use .text to get the text from the tag.

oname = soup.find("title")
oname.text

或只是 soup.title.text

In [4]: from bs4 import BeautifulSoup    
In [5]: import  requests
In [6]: r = requests.get("http://stackoverflow.com/questions/27934387/how-to-retrieve-information-inside-a-tag-with-python/27934403#27934387")    
In [7]: BeautifulSoup(r.content).title.text
Out[7]: u'html - How to Retrieve information inside a tag with python - Stack Overflow'

要打开一个文件并使用文本作为名字,就像使用其他任何字符串一样简单地使用它:

To open a file and use the text as the name simple use it as you would any other string:

with open(oname.text, 'w') as f

这篇关于如何使用BeautifulSoup bs4获取HTML标签的内部文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆