使用BeautifulSoup拉标签值 [英] Pull Tag Value using BeautifulSoup

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

问题描述

有人能告诉我如何用拉一个BeautifulSoup标签的价值?我阅读文档,但都很难通过它进行导航。举例来说,如果我有:

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:

<span title="Funstuff" class="thisClass">Fun Text</span>

怎么会随便拉Funstuff校车接送BeautifulSoup / Python的?

How would I just pull "Funstuff" busing BeautifulSoup/Python?

编辑:我使用的版本3.2.1

I am using version 3.2.1

推荐答案

您需要有东西来确定你要找的元素,它是很难分辨它是什么,在这个问题上。

You need to have something to identify the element you're looking for, and it's hard to tell what it is in this question.

例如,这两个将在BeautifulSoup 3.打印出Funstuff'一个查找span元素,并获得冠军,另一查找与给定类的跨度。许多其他有效的方式来得到这一点是可能的。

For example, both of these will print out 'Funstuff' in BeautifulSoup 3. One looks for a span element and gets the title, another looks for spans with the given class. Many other valid ways to get to this point are possible.

import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup('<html><body><span title="Funstuff" class="thisClass">Fun Text</span></body></html>')
print soup.html.body.span['title']
print soup.find('span', {"class": "thisClass"})['title']

这篇关于使用BeautifulSoup拉标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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