Search and Replace in HTML with BeautifulSoup?

This will insert a br tag after the end of each a>...') because BeautifulSoup doesn't operate on the end tags separately - they are considered part of the same element If you wanted to put the a elements inside a p element as you ask in a comment, you can use this: for a in soup. FindAll('a'): p = Tag(soup, 'p') #create a P element a. ReplaceWith(p) #Put it where the A element is p.

Insert(0, a) #put the A element inside the P (between and ) Again, you don't create the p and p separately because they are part of the same thing.

This will insert a tag after the end of each ... element: from BeautifulSoup import BeautifulSoup, Tag # .... soup = BeautifulSoup(data) for a in soup. FindAll('a'): a.parent. Insert(a.parent.

Index(a)+1, Tag(soup, 'br')) You can't use soup. FindAll(tag = '') because BeautifulSoup doesn't operate on the end tags separately - they are considered part of the same element. If you wanted to put the elements inside a element as you ask in a comment, you can use this: for a in soup.

FindAll('a'): p = Tag(soup, 'p') #create a P element a. ReplaceWith(p) #Put it where the A element is p. Insert(0, a) #put the A element inside the P (between and ) Again, you don't create the and separately because they are part of the same thing.

– Kevin Jan 15 '10 at 17:57 See my edit - It will be added after the whole ... element, so effectively, it will be only after the . – interjay Jan 15 '10 at 17:59 Is BeautifulSoup. Tag valid?

I am getting an error when trying this code. – Kevin Jan 15 '10 at 20:43 It depends on how you import the module. I edited to show one way it can work - try it now.

– interjay Jan 15 '10 at 21:38 FWIW it should be a.parent.content. Index – Lombo Jan 15 '107 at 23:13.

You don't replace an end-tag; in BeautifulSoup you are dealing with a document object model like in a browser, not a string full of HTML. So you couldn't ‘replace’ an end-tag without also replacing the start-tag. What you want to do is insert a new element immediately after the ... element.To do so you'll need to find out the index of the element inside its parent element, and insert the new element just after that index.

Eg. Soup= BeautifulSoup('blah blah blah') for link in soup. FindAll('a'): br= Tag(soup, 'br') index= link.parent.contents.

Index(link) link.parent. Insert(index+1, br) # soup now serialises to 'blah blah blah.

Would I be able to add tags before with a -1. Say I wanted to do and . Could I but the before using index -1 and after using +1?

– Kevin Jan 15 '10 at 20:30 You'd add an element before the chosen element using just index, not plus or minus anything. – bobince Jan 15 '10 at 22:04.

BeautifulSoup or easy_install BeautifulSoup. Ubuntu, and as python-BeautifulSoup on Red Hat. BeautifulSoup.py in your project directly.

To highlight a few high-profile projects. York Times building, uses Beautiful Soup to scrape news feeds.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions