Skip to content

Commit

Permalink
Add async iterator to HTML class
Browse files Browse the repository at this point in the history
  • Loading branch information
oldani authored and pigna90 committed Sep 19, 2018
1 parent 2d4c58d commit a25b373
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,17 @@ def __iter__(self):
def __next__(self):
return self._next(fetch=True, next_symbol=self.next_symbol).html

def __aiter__(self):
return self

async def __anext__(self):
while True:
url = self._next(fetch=False, next_symbol=self.next_symbol)
if not url:
break
response = await self.session.get(url)
return response.html

def add_next_symbol(self, next_symbol):
self.next_symbol.append(next_symbol)

Expand Down
18 changes: 17 additions & 1 deletion tests/test_internet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from requests_html import HTMLSession
import pytest
from requests_html import HTMLSession, AsyncHTMLSession

session = HTMLSession()


def test_pagination():
pages = (
'https://xkcd.com/1957/',
Expand All @@ -14,3 +16,17 @@ def test_pagination():
r = session.get(page)
assert next(r.html)


@pytest.mark.asyncio
async def test_pagination(event_loop):
asession = AsyncHTMLSession()
pages = (
'https://xkcd.com/1957/',
'https://reddit.com/',
'https://smile.amazon.com/',
'https://theverge.com/archives'
)

for page in pages:
r = await asession.get(page)
assert await r.html.__anext__()

0 comments on commit a25b373

Please sign in to comment.