简体   繁体   中英

BeautifulSoup 4 - How to replace anchor tag with it's text

I have html content in my document. I need to replace all the anchor tags with their respective texts using BeautifulSoup .

My input is

html = '''They are also much more fuel-efficient than <a href="http://someurl.com">rockets</a>.'''

Expected output

"They are also much more fuel-efficient than rockets."

Here is my code

soup = BeautifulSoup(html, 'html.parser')
for a in soup.find_all('a'):
    ...
    replacement_string = a.string
    //I get all the anchor tags here. I need to perform the replace operation here
    ...
//Should display 'They are also much more fuel-efficient than rockets.'
print(replaced_html_string) 

I was able to replace the elements of the anchor tag but not the whole tag itself.

You don't really need to separate all the tags out to get the text. just use .text :

soup = BeautifulSoup(html, 'html.parser')
print(soup.text)

gives:

'They are also much more fuel-efficient than rockets.'

Or in your way:

res = str(soup)
for i in soup.find_all('a'):
    res = res.replace(str(i),i.text)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM