无法将请求写入文件的 html 写入文件

问题描述

我是 Python 新手。
我正在尝试将通过请求和 BeautifulSoup 获得的 html 写入文件,以便我可以更好地阅读它,
但我刚收到这个错误

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

这是我正在运行的代码

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())

打印它给我我想要的。

解决方法

在soup中,find函数返回const PORT = process.env.PORT || 3000; app.listen(PORT,() => { console.log(`Our app is running on port ${ PORT }`); }); ,为了打印它首先你应该把它转换成String。所以你应该试试这个:

Tag
,

这应该有效:

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