Search code examples
pythonweb-scrapingfile-writing

Can't write html got with requests to a file


I'm new to python.
I'm trying to write the html I get with requests and BeautifulSoup to a file so I can read it better,
but I just get this error:

File "C:\python\manga\manga.py", line 12, in <module>
    html.write(results)
TypeError: write() argument must be str, not Tag

Here is the code I'm running:

import requests
from bs4 import BeautifulSoup

URL = 'https://mangapark.net/manga/tensei-shitara-slime-datta-ken-fuse'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')

results = soup.find('section', class_='manga')

with open('html.txt', 'w') as html:
    html.write(results)

# print(results.prettify())

Printing it gives me what I want.


Solution

  • This should work:

    with open('html.txt', 'w', encoding='utf-8') as html:
        html.write(str(results))