Skip to content

Commit c12d7c6

Browse files
committed
Add async iterator to HTML class
1 parent dd05a02 commit c12d7c6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

requests_html.py

+11
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ def __iter__(self):
485485
def __next__(self):
486486
return self._next(fetch=True, next_symbol=self.next_symbol).html
487487

488+
def __aiter__(self):
489+
return self
490+
491+
async def __anext__(self):
492+
while True:
493+
url = self._next(fetch=False, next_symbol=self.next_symbol)
494+
if not url:
495+
break
496+
response = await self.session.get(url)
497+
return response.html
498+
488499
def add_next_symbol(self, next_symbol):
489500
self.next_symbol.append(next_symbol)
490501

tests/test_internet.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from requests_html import HTMLSession
1+
import pytest
2+
from requests_html import HTMLSession, AsyncHTMLSession
23

34
session = HTMLSession()
45

6+
57
def test_pagination():
68
pages = (
79
'https://xkcd.com/1957/',
@@ -14,3 +16,17 @@ def test_pagination():
1416
r = session.get(page)
1517
assert next(r.html)
1618

19+
20+
@pytest.mark.asyncio
21+
async def test_pagination(event_loop):
22+
asession = AsyncHTMLSession()
23+
pages = (
24+
'https://xkcd.com/1957/',
25+
'https://reddit.com/',
26+
'https://smile.amazon.com/',
27+
'https://theverge.com/archives'
28+
)
29+
30+
for page in pages:
31+
r = await asession.get(page)
32+
assert await r.html.__anext__()

0 commit comments

Comments
 (0)