Advertisement
losmi93

b sobe html edit 1

Oct 18th, 2023
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2.  
  3. # Parse the HTML file
  4. soup = BeautifulSoup(open('example.html'), 'html.parser')
  5.  
  6. # Find the link with the specified alt attribute
  7. link = soup.find('a', {'alt': 'Hello world'})
  8.  
  9. # Find the img tag in the link
  10. img = link.find('img')
  11.  
  12. # Change the src attribute of the img tag
  13. img['src'] = 'https://example.com/new-image.jpg'
  14.  
  15.  
  16. # Replace the link with the replacement string
  17. link.string = 'Goodbye world'
  18.  
  19. # Write the modified HTML back to the file
  20. with open('example.html', 'w') as f:
  21.     f.write(str(soup))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement