Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- # Parse the HTML file
- soup = BeautifulSoup(open('example.html'), 'html.parser')
- # Find the link with the specified alt attribute
- link = soup.find('a', {'alt': 'Hello world'})
- # Find the img tag in the link
- img = link.find('img')
- # Change the src attribute of the img tag
- img['src'] = 'https://example.com/new-image.jpg'
- # Replace the link with the replacement string
- link.string = 'Goodbye world'
- # Write the modified HTML back to the file
- with open('example.html', 'w') as f:
- f.write(str(soup))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement