0

I've been stumbling upon a problem with my code, I want to use a library to open a web page's contents and view them in a specific way, after viewing the source code for that lib, I figured out that to use this lib, I needed to use a _io.TextIOWrapper object and not a aiohttp object, so I was wondering if there is any way to convert the too. Heres some examples

>>> open('./NEWS.txt')
<_io.TextIOWrapper name='./NEWS.txt' mode='r' encoding='cp1252'>
>>> import aiohttp
>>> import asyncio
>>> async def fetch(session, url):
...     async with session.get(url) as response:
...         return response
...
>>> async def main():
...     async with aiohttp.ClientSession() as session:
...         html = await fetch(session, 'http://python.org')
...         print(html)
...
>>> if __name__ == '__main__':
...     loop = asyncio.get_event_loop()
...     loop.run_until_complete(main())
...
<ClientResponse(https://www.python.org/) [200 OK]>
<CIMultiDictProxy('Server': 'nginx', 'Content-Type': 'text/html; charset=utf-8', 'X-Frame-Options': 'DENY', 'Via': '1.1 vegur', 'Via': '1.1 varnish', 'Content-Length': '49058', 'Accept-Ranges': 'bytes', 'Date': 'Fri, 08 May 2020 14:20:23 GMT', 'Via': '1.1 varnish', 'Age': '1960', 'Connection': 'keep-alive', 'X-Served-By': 'cache-bwi5137-BWI, cache-pao17432-PAO', 'X-Cache': 'HIT, HIT', 'X-Cache-Hits': '2, 2', 'X-Timer': 'S1588947623.222259,VS0,VE0', 'Vary': 'Cookie', 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains')>

>>>


Any thoughts? Please let me know

Artrix
  • 149
  • 10

1 Answers1

1

TextIOWrapper takes a buffer as first argument. Since your response is just a string, you can convert the string to a buffered reader and pass it to TextIOWrapper.

import io
html = await response.text()
buffer = io.BufferedReader(io.BytesIO(html.encode("utf-8")))
textWrapper = io.TextIOWrapper(buffer)
print(textWrapper.read())

Now there could be a better way. aiohttp offeres you to decode the response directly to bytes. If you could use that somehow it would be more efficient, since you dont have to convert to text in between.

async with session.get('https://api.github.com/events') as resp:
    await resp.content.read()

Here check the documentation on this.

The Fool
  • 16,715
  • 5
  • 52
  • 86