I am trying to parse a website with the following HTML.
I am using Python and BeautifulSoup.
How do I extract the text Texas Rangers out of this?
I am having trouble since it is not in a class? Thanks,
Matt
<div class="team">
<span class="team-logo mlb tex"></span>Texas Rangers
<br />
<a class="fancy" href="/split_stats/index/Baseball/Pitcher/107">BvP</a>
·
<a class="fancy" href="/split_stats/index/Baseball/Righty/107">vs. R/a>
·
<a class="fancy" href="/split_stats/index/Baseball/Away/107">Away</a>
·
<a class="fancy" href="/split_stats/index/Baseball/Night/107">Night</a>
</div>
May not be the best solution but this works.
>>> soup = BeautifulSoup(htmlCode)
>>> soup.div.contents[2].strip()
u'Texas Rangers'
I would use the following code that I ran within ipython:
In [28]: htmldoc = """<div class="team">
....: <span class="team-logo mlb tex"></span>Texas Rangers
....: <br />
....: <a class="fancy" href="/split_stats/index/Baseball/Pitcher/107">BvP</a>
....: ·
....: <a class="fancy" href="/split_stats/index/Baseball/Righty/107">vs. R/a>
....: ·
....: </a><a class="fancy" href="/split_stats/index/Baseball/Away/107">Away</a>
....: ·
< ....: <a class="fancy" href="/split_stats/index/Baseball/Night/107">Night</a>
....: </div>
....: """
In [30]: soup = BeautifulSoup(htmldoc)
In [31]: import re
In [32]: soup(text=re.compile('Texas Rangers'))
Out[32]: [u'Texas Rangers\n']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.