mirror of
https://github.com/kennethreitz/requests-html.git
synced 2026-06-05 23:00:20 +00:00
Add async iterator to HTML class
This commit is contained in:
committed by
Alessandro Romano
parent
2d4c58deb8
commit
a25b3737ad
@@ -485,6 +485,17 @@ class HTML(BaseParser):
|
||||
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)
|
||||
|
||||
|
||||
+17
-1
@@ -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/',
|
||||
@@ -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__()
|
||||
|
||||
Reference in New Issue
Block a user