-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyzer): support items_selector in infinite scroll detection
- Loading branch information
Showing
4 changed files
with
112 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,25 @@ | ||
import asyncio | ||
import json | ||
from textwrap import indent | ||
|
||
from npiai.tools.web.scraper import Scraper | ||
from npiai.tools.web.page_analyzer import PageAnalyzer | ||
|
||
# from npiai.utils.test_utils import DebugContext | ||
from npiai import Context | ||
from npiai.tools.web.page_analyzer import PageAnalyzer | ||
from npiai.tools.web.scraper import Scraper | ||
from utils import autos_scrape | ||
|
||
|
||
async def main(): | ||
url = input("Enter the URL: ") | ||
ctx = Context() | ||
|
||
async with PageAnalyzer(headless=False) as analyzer: | ||
scraper = Scraper(batch_size=10, playwright=analyzer.playwright) | ||
|
||
print(f"Analyzing {url}:") | ||
|
||
infinite_scroll = await analyzer.support_infinite_scroll( | ||
url=url, | ||
) | ||
|
||
print(" - Support infinite scroll:", infinite_scroll) | ||
|
||
pagination = await analyzer.get_pagination_button( | ||
ctx=ctx, | ||
url=url, | ||
) | ||
|
||
print(" - Pagination button:", pagination) | ||
|
||
scraping_type = await analyzer.infer_scraping_type( | ||
ctx=ctx, | ||
await autos_scrape( | ||
ctx=Context(), | ||
analyzer=analyzer, | ||
scraper=scraper, | ||
url=url, | ||
) | ||
|
||
print(" - Inferred scraping type:", scraping_type) | ||
|
||
if scraping_type == "list-like": | ||
selectors = await analyzer.infer_similar_items_selector(ctx, url) | ||
|
||
print( | ||
" - Possible selectors:", | ||
indent(json.dumps(selectors, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
if not selectors: | ||
return | ||
|
||
columns = await scraper.infer_columns( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type="list-like", | ||
ancestor_selector=selectors["ancestor"], | ||
items_selector=selectors["items"], | ||
) | ||
|
||
print( | ||
" - Inferred columns:", | ||
indent(json.dumps(columns, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
stream = scraper.summarize_stream( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type="list-like", | ||
ancestor_selector=selectors["ancestor"], | ||
items_selector=selectors["items"], | ||
output_columns=columns, | ||
limit=10, | ||
) | ||
|
||
async for items in stream: | ||
print("Chunk:", json.dumps(items, indent=2, ensure_ascii=False)) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,22 @@ | ||
import asyncio | ||
import json | ||
from textwrap import indent | ||
|
||
from npiai.tools.web.scraper import Scraper | ||
from npiai.tools.web.page_analyzer import PageAnalyzer | ||
from npiai.tools.web.twitter import Twitter | ||
|
||
# from npiai.utils.test_utils import DebugContext | ||
from npiai import Context | ||
|
||
url = "https://x.com/home" | ||
from npiai.tools.web.page_analyzer import PageAnalyzer | ||
from npiai.tools.web.scraper import Scraper | ||
from npiai.tools.web.twitter import Twitter | ||
from utils import autos_scrape | ||
|
||
|
||
async def main(): | ||
ctx = Context() | ||
|
||
async with Twitter(headless=False) as twitter: | ||
analyzer = PageAnalyzer(playwright=twitter.playwright) | ||
scraper = Scraper(batch_size=10, playwright=twitter.playwright) | ||
|
||
print(f"Analyzing {url}:") | ||
|
||
infinite_scroll = await analyzer.support_infinite_scroll( | ||
url=url, | ||
) | ||
|
||
print(" - Support infinite scroll:", infinite_scroll) | ||
|
||
pagination = await analyzer.get_pagination_button( | ||
ctx=ctx, | ||
url=url, | ||
await autos_scrape( | ||
ctx=Context(), | ||
analyzer=PageAnalyzer(playwright=twitter.playwright), | ||
scraper=Scraper(batch_size=10, playwright=twitter.playwright), | ||
url="https://x.com/home", | ||
) | ||
|
||
print(" - Pagination button:", pagination) | ||
|
||
scraping_type = await analyzer.infer_scraping_type( | ||
ctx=ctx, | ||
url=url, | ||
) | ||
|
||
print(" - Inferred scraping type:", scraping_type) | ||
|
||
if scraping_type == "list-like": | ||
selectors = await analyzer.infer_similar_items_selector(ctx, url) | ||
|
||
print( | ||
" - Possible selectors:", | ||
indent(json.dumps(selectors, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
if not selectors: | ||
return | ||
|
||
columns = await scraper.infer_columns( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type="list-like", | ||
ancestor_selector=selectors["ancestor"], | ||
items_selector=selectors["items"], | ||
) | ||
|
||
print( | ||
" - Inferred columns:", | ||
indent(json.dumps(columns, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
stream = scraper.summarize_stream( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type="list-like", | ||
ancestor_selector=selectors["ancestor"], | ||
items_selector=selectors["items"], | ||
output_columns=columns, | ||
limit=10, | ||
) | ||
|
||
async for items in stream: | ||
print("Chunk:", json.dumps(items, indent=2, ensure_ascii=False)) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import json | ||
from textwrap import indent | ||
|
||
from npiai.tools.web.scraper import Scraper | ||
from npiai.tools.web.page_analyzer import PageAnalyzer | ||
|
||
# from npiai.utils.test_utils import DebugContext | ||
from npiai import Context | ||
|
||
|
||
async def autos_scrape( | ||
ctx: Context, | ||
analyzer: PageAnalyzer, | ||
scraper: Scraper, | ||
url: str, | ||
): | ||
ancestor_selector = None | ||
items_selector = None | ||
|
||
print(f"Analyzing {url}:") | ||
|
||
scraping_type = await analyzer.infer_scraping_type( | ||
ctx=ctx, | ||
url=url, | ||
) | ||
|
||
print(" - Inferred scraping type:", scraping_type) | ||
|
||
if scraping_type == "list-like": | ||
selectors = await analyzer.infer_similar_items_selector(ctx, url) | ||
|
||
print( | ||
" - Possible selectors:", | ||
indent(json.dumps(selectors, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
if selectors: | ||
ancestor_selector = selectors["ancestor"] | ||
items_selector = selectors["items"] | ||
|
||
infinite_scroll = await analyzer.support_infinite_scroll( | ||
url=url, | ||
items_selector=items_selector, | ||
) | ||
|
||
print(" - Support infinite scroll:", infinite_scroll) | ||
|
||
pagination_button_selector = await analyzer.get_pagination_button( | ||
ctx=ctx, | ||
url=url, | ||
) | ||
|
||
print(" - Pagination button:", pagination_button_selector) | ||
|
||
columns = await scraper.infer_columns( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type=scraping_type, | ||
ancestor_selector=ancestor_selector, | ||
items_selector=items_selector, | ||
) | ||
|
||
print( | ||
" - Inferred columns:", | ||
indent(json.dumps(columns, indent=2), prefix=" ").lstrip(), | ||
) | ||
|
||
stream = scraper.summarize_stream( | ||
ctx=ctx, | ||
url=url, | ||
scraping_type=scraping_type, | ||
ancestor_selector=ancestor_selector, | ||
items_selector=items_selector, | ||
pagination_button_selector=pagination_button_selector, | ||
output_columns=columns, | ||
limit=1 if scraping_type == "single" else 10, | ||
) | ||
|
||
async for items in stream: | ||
print("Chunk:", json.dumps(items, indent=2, ensure_ascii=False)) |