Beautifulsoup convert string to ResultSet object of bs4.element module

Just create another BeautifulSoup object:

import requests
from bs4 import BeautifulSoup

#scraping
website_link = 'https://stackoverflow.com/'
request1 = requests.get(website_link)
source1 = request1.content
soup1 = BeautifulSoup(source1, 'html.parser')


#saving
savefilename = 'question.txt'
with open(savefilename, "w", encoding="utf-8") as f:
    f.write(str(soup1))

# Open the saved file
with open(savefilename, "r", encoding="utf-8") as f:
    soup2 = BeautifulSoup(str(f.readlines()), "html.parser")
    
>>> print(type(soup2))
class 'bs4.BeautifulSoup'>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top