Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Modifying and adding tags
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bruce Eckel  
View profile  
 More options Jan 23 2012, 11:13 am
From: Bruce Eckel <brucetec...@gmail.com>
Date: Mon, 23 Jan 2012 08:13:27 -0800 (PST)
Local: Mon, Jan 23 2012 11:13 am
Subject: Modifying and adding tags
I'm trying to change certain "p" tags to "span" tags and to append a
"br" at the end of the outer span. I've been thrashing about for
awhile, going through the BeautifulSoup documentation, the unit tests
and scanning the source code, and the following is as far as I've
gotten. My questions:

1) How do I get the "br" tag to appear AFTER the outer span?

2) Am I approaching this problem correctly or is there a more elegant
way to do this?

3) Optional bonus question -- should I just be combining the spans
somehow, and is there a good way to do that?

Thanks for any help...

Here's the example:

from BeautifulSoup import BeautifulSoup, Tag

soup = BeautifulSoup("""
<html>
 <body>
  <p class="c1">
   <span class="c5">
    1 &nbsp; // Values.scala
   </span>
  </p>
  <p class="c1">
   <span class="c5">
    2 &nbsp;
   </span>
  </p>
  <p class="c1">
   <span class="c5">
    3 &nbsp; val anInteger: Int = 11
   </span>
  </p>
  <p class="c1">
   <span class="c5">
    4 &nbsp; val aDouble: Double = 1.4
   </span>
  </p>
  <p class="c1">
   <span class="c5">
    5 &nbsp; // true or false:
   </span>
  </p>
  <p class="c1">
   <span class="c5">
    6 &nbsp; val trueOrFalse: Boolean = true
   </span>
  </p>
 </body>
</html>
""")

for e in soup.findAll("p", { "class" : "c1" }):
    print e; print
    br = Tag(soup, "br")
    span = Tag(soup, "span", [("class", "c1")])
    span.insert(0, e.renderContents())
    span.append(br)
    print span
    e.replaceWith(span)
    print "***"
print(soup)

------------------------------------- And here's the output:
------------------------------------------

<p class="c1">
<span class="c5">
    1 &nbsp; // Values.scala
   </span>
</p>

<span class="c1">
<span class="c5">
    1 &nbsp; // Values.scala
   </span>
<br /></span>
***
<p class="c1">
<span class="c5">
    2 &nbsp;
   </span>
</p>

<span class="c1">
<span class="c5">
    2 &nbsp;
   </span>
<br /></span>
***
<p class="c1">
<span class="c5">
    3 &nbsp; val anInteger: Int = 11
   </span>
</p>

<span class="c1">
<span class="c5">
    3 &nbsp; val anInteger: Int = 11
   </span>
<br /></span>
***
<p class="c1">
<span class="c5">
    4 &nbsp; val aDouble: Double = 1.4
   </span>
</p>

<span class="c1">
<span class="c5">
    4 &nbsp; val aDouble: Double = 1.4
   </span>
<br /></span>
***
<p class="c1">
<span class="c5">
    5 &nbsp; // true or false:
   </span>
</p>

<span class="c1">
<span class="c5">
    5 &nbsp; // true or false:
   </span>
<br /></span>
***
<p class="c1">
<span class="c5">
    6 &nbsp; val trueOrFalse: Boolean = true
   </span>
</p>

<span class="c1">
<span class="c5">
    6 &nbsp; val trueOrFalse: Boolean = true
   </span>
<br /></span>
***

<html>
<body>
<span class="c1">
<span class="c5">
    1 &nbsp; // Values.scala
   </span>
<br /></span>
<span class="c1">
<span class="c5">
    2 &nbsp;
   </span>
<br /></span>
<span class="c1">
<span class="c5">
    3 &nbsp; val anInteger: Int = 11
   </span>
<br /></span>
<span class="c1">
<span class="c5">
    4 &nbsp; val aDouble: Double = 1.4
   </span>
<br /></span>
<span class="c1">
<span class="c5">
    5 &nbsp; // true or false:
   </span>
<br /></span>
<span class="c1">
<span class="c5">
    6 &nbsp; val trueOrFalse: Boolean = true
   </span>
<br /></span>
</body>
</html>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruce Eckel  
View profile  
 More options Jan 25 2012, 5:12 pm
From: Bruce Eckel <brucetec...@gmail.com>
Date: Wed, 25 Jan 2012 15:12:02 -0700
Local: Wed, Jan 25 2012 5:12 pm
Subject: Modifying and adding tags

I'm trying to change certain "p" tags to "span" tags and to append a
"br" at the end of the outer span. I've been thrashing about for
awhile, going through the BeautifulSoup documentation, the unit tests
and scanning the source code, and the following is as far as I've
gotten. My questions:

1) How do I get the "br" tag to appear AFTER the outer span?

2) Am I approaching this problem correctly or is there a more elegant
way to do this?

3) Optional bonus question -- should I just be combining the spans
somehow, and is there a good way to do that?

Thanks for any help...

Here's the example:

from BeautifulSoup import BeautifulSoup, Tag

soup = BeautifulSoup("""
<html>
 <body>
 <p class="c1">
  <span class="c5">
   1 &nbsp; // Values.scala
  </span>
 </p>
 <p class="c1">
  <span class="c5">
   2 &nbsp;
  </span>
 </p>
 <p class="c1">
  <span class="c5">
   3 &nbsp; val anInteger: Int = 11
  </span>
 </p>
 <p class="c1">
  <span class="c5">
   4 &nbsp; val aDouble: Double = 1.4
  </span>
 </p>
 <p class="c1">
  <span class="c5">
   5 &nbsp; // true or false:
  </span>
 </p>
 <p class="c1">
  <span class="c5">
   6 &nbsp; val trueOrFalse: Boolean = true
  </span>
 </p>
 </body>
</html>
""")

for e in soup.findAll("p", { "class" : "c1" }):
   print e; print
   br = Tag(soup, "br")
   span = Tag(soup, "span", [("class", "c1")])
   span.insert(0, e.renderContents())
   span.append(br)
   print span
   e.replaceWith(span)
   print "***"
print(soup)

------------------------------------- And here's the output:
------------------------------------------

<p class="c1">
<span class="c5">
   1 &nbsp; // Values.scala
  </span>
</p>

<span class="c1">
<span class="c5">
   1 &nbsp; // Values.scala
  </span>
<br /></span>
***
<p class="c1">
<span class="c5">
   2 &nbsp;
  </span>
</p>

<span class="c1">
<span class="c5">
   2 &nbsp;
  </span>
<br /></span>
***
<p class="c1">
<span class="c5">
   3 &nbsp; val anInteger: Int = 11
  </span>
</p>

<span class="c1">
<span class="c5">
   3 &nbsp; val anInteger: Int = 11
  </span>
<br /></span>
***
<p class="c1">
<span class="c5">
   4 &nbsp; val aDouble: Double = 1.4
  </span>
</p>

<span class="c1">
<span class="c5">
   4 &nbsp; val aDouble: Double = 1.4
  </span>
<br /></span>
***
<p class="c1">
<span class="c5">
   5 &nbsp; // true or false:
  </span>
</p>

<span class="c1">
<span class="c5">
   5 &nbsp; // true or false:
  </span>
<br /></span>
***
<p class="c1">
<span class="c5">
   6 &nbsp; val trueOrFalse: Boolean = true
  </span>
</p>

<span class="c1">
<span class="c5">
   6 &nbsp; val trueOrFalse: Boolean = true
  </span>
<br /></span>
***

<html>
<body>
<span class="c1">
<span class="c5">
   1 &nbsp; // Values.scala
  </span>
<br /></span>
<span class="c1">
<span class="c5">
   2 &nbsp;
  </span>
<br /></span>
<span class="c1">
<span class="c5">
   3 &nbsp; val anInteger: Int = 11
  </span>
<br /></span>
<span class="c1">
<span class="c5">
   4 &nbsp; val aDouble: Double = 1.4
  </span>
<br /></span>
<span class="c1">
<span class="c5">
   5 &nbsp; // true or false:
  </span>
<br /></span>
<span class="c1">
<span class="c5">
   6 &nbsp; val trueOrFalse: Boolean = true
  </span>
<br /></span>
</body>
</html>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »