Reputation: 7043
I scraped one container which includes urls for example:
<a href="url">text</a>
I need all to be removed and only the text remain...
import urllib2, sys
from bs4 import BeautifulSoup
site = "http://mysite.com"
page = urllib2.urlopen(site)
soup = BeautifulSoup(page)
Is it possible?
Upvotes: 2
Views: 2507
Reputation: 10163
soup = BeautifulSoup(page)
anchors = soup.findAll('a')
for anchor in anchors:
anchor.replaceWithChildren()
Upvotes: 4
Reputation: 15680
You can do this with Bleach
>>> import bleach
>>> bleach.clean('an <script>evil()</script> example')
u'an <script>evil()</script> example'
>>> bleach.linkify('an http://example.com url')
u'an <a href="http://example.com" rel="nofollow">http://example.com</a> url
>>> bleach.delinkify('a <a href="http://ex.mp">link</a>')
u'a link'
Upvotes: 6