提取链接标签之间的文本使用BeautifulSoup蟒蛇 [英] Extract text between link tags in python using BeautifulSoup

查看:176
本文介绍了提取链接标签之间的文本使用BeautifulSoup蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html code是这样的:

I have an html code like this:

< H2类=标题>< A HREF =htt​​p://www.gurletins.com>我的首页< / A>< / H>

< H2类=标题>< A HREF =htt​​p://www.gurletins.com/sections>节< / A>< / H>

我要'一'标记之间提取文本(链接的描述)。我需要一个数组来存储这些,如:

I need to extract the texts (link descriptions) between 'a' tags. I need an array to store these like:

A [0] =我的主页

a[0] = "My HomePage"

A [1] =节

我需要使用BeautifulSoup做到这一点的蟒蛇。

I need to do this in python using BeautifulSoup.

请帮帮我,谢谢!

推荐答案

您可以做这样的事情:

import BeautifulSoup

html = """
<html><head></head>
<body>
<h2 class='title'><a href='http://www.gurletins.com'>My HomePage</a></h2>
<h2 class='title'><a href='http://www.gurletins.com/sections'>Sections</a></h2>
</body>
</html>
"""

soup = BeautifulSoup.BeautifulSoup(html)

print [elm.a.text for elm in soup.findAll('h2', {'class': 'title'})]
# Output: [u'My HomePage', u'Sections']

这篇关于提取链接标签之间的文本使用BeautifulSoup蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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