所以我试图刮掉整个页面。我希望两者都能正常工作。所以这是不起作用的代码:
import aiohttp
import asyncio
url = "https://unsplash.com/s/photos/dogs"
async def main():
async with aiohttp.ClientSession() as s:
async with s.get(url) as r:
enc = str(r.get_encoding())
bytes = await r.read() <--- returns <class 'bytes'>
with open("stuff.html", "w") as f:
f.write(bytes.decode(encoding=enc, errors="ignore")) <-- in errors I've tried all possible accepted values.
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
这导致UnicodeEncodeError: 'charmap' codec can't encode character '\u2713' in position 58100: character maps to <undefined>
. 我假设是在指定位置的一个字符,由于某种原因无法解码并转换为字符串。通过将main
函数修改为以下内容,它可以正常工作。
async def main():
async with aiohttp.ClientSession() as s:
async with s.get(url) as r:
enc = str(r.get_encoding())
bytes = await r.read()
with open("stuf.html", "wb") as f:
f.write(bytes)
我不确定为什么它不起作用。因为在第二个代码块中,我只是将字节写入stuff.html
使用上下文管理器调用的文件中。并在第一个代码块中。我只是用更长的方法来做同样的事情decode()
,很好地解码它并将其转换为要写入文件的字符串。所以我不需要用wb
orw
等打开文件。