473,800 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BeautifulSoup to get string inner 'p' and 'a' tags

I'm trying to get the 'FOO' string but the problem is that inner 'P'
tag there is another tag, 'a'. So:
from BeautifulSoup import BeautifulSoup
s = '<td width="88%" valign="TOP"<p class="contentB ody">FOO <a name="f"></a</p></td>'
tree = BeautifulSoup(s )
print tree.first('p')
<p class="contentB ody">FOO <a name="f"></a</p>

So if I run 'print tree.first('p') .string' to get the 'FOO' string it
shows Null value because it's the 'a' tag:
print tree.first('p') .string
Null

Any solution?

Jul 24 '06 #1
3 5848
In <11************ **********@i42g 2000cwa.googleg roups.com>, GinTon wrote:
I'm trying to get the 'FOO' string but the problem is that inner 'P'
tag there is another tag, 'a'. So:
>from BeautifulSoup import BeautifulSoup
s = '<td width="88%" valign="TOP"<p class="contentB ody">FOO <a name="f"></a</p></td>'
tree = BeautifulSoup(s )
>print tree.first('p')
<p class="contentB ody">FOO <a name="f"></a</p>

So if I run 'print tree.first('p') .string' to get the 'FOO' string it
shows Null value because it's the 'a' tag:
>print tree.first('p') .string
Null

Any solution?
In [53]: print tree.first('p') .contents[0]
FOO

Ciao,
Marc 'BlackJack' Rintsch
Jul 24 '06 #2

Marc 'BlackJack' Rintsch wrote:
In [53]: print tree.first('p') .contents[0]
FOO
Thanks! I was going to crazy with this.

Jul 24 '06 #3
Quick-n-dirty way:
After you get your whole p string: <p class="contentB ody">FOO <a
name="f"></a</p>
Remove any tags delimited by '<' and '>' with a regex. In your short
example you _don't_ show that there might be something between the <a>
and </atags so I assume there won't be anything or if there would be
something then you also want it included in the final text. As in
'<p class="contentB ody">FOO <a name="f">URLNAM E</a</p>' =='FOO
URLNAME'

For the regex start with something simple like <.*?and see if it
works then improve it. Use kiki or kodos - python visual regex
helpers.

Hope this helps,
Nick V.
GinTon wrote:
I'm trying to get the 'FOO' string but the problem is that inner 'P'
tag there is another tag, 'a'. So:
from BeautifulSoup import BeautifulSoup
s = '<td width="88%" valign="TOP"<p class="contentB ody">FOO <a name="f"></a</p></td>'
tree = BeautifulSoup(s )
print tree.first('p')
<p class="contentB ody">FOO <a name="f"></a</p>

So if I run 'print tree.first('p') .string' to get the 'FOO' string it
shows Null value because it's the 'a' tag:
print tree.first('p') .string
Null

Any solution?
Jul 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2443
by: ted | last post by:
Hi, I'm using the BeautifulSoup module and having some trouble processing a file. It's not printing what I'm expecting. In the code below, I'm expecting cells with only "bgcolor" attributes to be printed, but I'm getting cells with other attributes and some without any attributes. Any help appreciated. Thanks, Ted
7
8610
by: Gonzillaaa | last post by:
I'm trying to get the data on the "Central London Property Price Guide" box at the left hand side of this page http://www.findaproperty.com/regi0018.html I have managed to get the data :) but when I start looking for tables I only get tables of depth 1 how do I go about accessing inner tables? same happens for links... this is what I've go so far
5
2663
by: John Nagle | last post by:
This, which is from a real web site, went into BeautifulSoup: <param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer fantastic rates for selected weeks or days!!&blinkt=Click here And this came out, via prettify: <addresssnippet siteurl="http%3A//apartmentsapart.com" url="http%3A//www.apartmentsapart.com/Europe/Spain/Madrid/FAQ"> <param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer
6
1892
by: John Nagle | last post by:
Here's a construct with which BeautifulSoup has problems. It's from "http://support.microsoft.com/contactussupport/?ws=support". This is the original: <a href="http://www.microsoft.com/usability/enroll.mspx" id="L_75998" title="<!--http://www.microsoft.com/usability/information.mspx->" onclick="return MS_HandleClick(this,'C_32179', true);">
11
3181
by: John Nagle | last post by:
The syntax that browsers understand as HTML comments is much less restrictive than what BeautifulSoup understands. I keep running into sites with formally incorrect HTML comments which are parsed happily by browsers. Here's yet another example, this one from "http://www.webdirectory.com". The page starts like this: <!Hello there! Welcome to The Environment Directory!> <!Not too much exciting HTML code here but it does the job! >...
0
1052
by: John Nagle | last post by:
This web page: http://azultralights.com/ulclass.html parses OK with BeautifulSoup, but "prettify" will hit the recursion limit if you try to display it. I raised the recursion limit to a large number, and it was converted to 5MB of text successfully, in about a minute. The page has real problems. 1901 errors from the W3C validator,
0
1078
by: Marco Hornung | last post by:
Hy guys, I'm using the python-framework BeautifulSoup(BS) to parse some information out of a german soccer-website. I spend some qualitiy time with the BS-docs, but I couldn't really figure out how to get what I was looking for. Here's the deal: I want to parse the article shown on the website. To do so I want to use the Tag " <div class="txt_fliesstext">" as a starting-point. When
3
3296
by: Jackie Wang | last post by:
Dear all, I have the following html code: <td valign="top" headers="col1"> <font size="2"> Center Bank <br /> Los Angeles, CA </font>
0
944
by: bruce | last post by:
hi jackie, if you don't mind... can i ask what you're looking to accomplish? are you looking to simply get the text/string data, or something else??? -----Original Message----- From: python-list-bounces+bedouglas=earthlink.net@python.org On Behalf
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.