-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add infinite scrolling #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VinciGit00
wants to merge
4
commits into
main
Choose a base branch
from
add-infinite-scrolling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7549ec4
feat: add infinite scrolling
VinciGit00 9c60b86
Update smartscraper_infinite_scroll_example.py
VinciGit00 56c3b2d
Update async_smartscraper_infinite_scroll_example.py
VinciGit00 fcc8dc3
refactor: simplify infinite scroll config by replacing scroll_options…
Vikrant-Khedkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
scrapegraph-py/examples/async/async_smartscraper_infinite_scroll_example.py
This file contains hidden or 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,64 @@ | ||
import asyncio | ||
from typing import List, Dict, Any | ||
|
||
from scrapegraph_py import AsyncClient | ||
from scrapegraph_py.logger import sgai_logger | ||
|
||
sgai_logger.set_logging(level="INFO") | ||
|
||
|
||
async def scrape_companies(client: AsyncClient, url: str, batch: str) -> None: | ||
"""Scrape companies from a specific YC batch with infinite scroll.""" | ||
try: | ||
# Initial scrape with infinite scroll enabled | ||
response = await client.smartscraper( | ||
website_url=url, | ||
user_prompt="Extract all company information from this page, including name, description, and website", | ||
number_of_scrolls=10, | ||
) | ||
|
||
# Process the results | ||
companies = response.get("result", []) | ||
if not companies: | ||
print(f"No companies found for batch {batch}") | ||
return | ||
|
||
# Save or process the companies data | ||
print(f"Found {len(companies)} companies in batch {batch}") | ||
for company in companies: | ||
print(f"Company: {company.get('name', 'N/A')}") | ||
print(f"Description: {company.get('description', 'N/A')}") | ||
print(f"Website: {company.get('website', 'N/A')}") | ||
print("-" * 50) | ||
|
||
except Exception as e: | ||
print(f"Error scraping batch {batch}: {str(e)}") | ||
|
||
|
||
async def main(): | ||
# Initialize async client | ||
client = AsyncClient(api_key="your-api-key-here") | ||
|
||
try: | ||
# Example YC batch URLs | ||
batch_urls = { | ||
"W24": "https://www.ycombinator.com/companies?batch=W24", | ||
"S23": "https://www.ycombinator.com/companies?batch=S23" | ||
} | ||
|
||
# Create tasks for each batch | ||
tasks = [ | ||
scrape_companies(client, url, batch) | ||
for batch, url in batch_urls.items() | ||
] | ||
|
||
# Execute all batch scraping concurrently | ||
await asyncio.gather(*tasks) | ||
|
||
finally: | ||
# Ensure client is properly closed | ||
await client.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
46 changes: 46 additions & 0 deletions
46
scrapegraph-py/examples/sync/smartscraper_infinite_scroll_example.py
This file contains hidden or 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,46 @@ | ||
from scrapegraph_py import Client | ||
from scrapegraph_py.logger import sgai_logger | ||
from pydantic import BaseModel | ||
from typing import List | ||
|
||
sgai_logger.set_logging(level="INFO") | ||
|
||
# Define the output schema | ||
class Company(BaseModel): | ||
name: str | ||
category: str | ||
location: str | ||
|
||
class CompaniesResponse(BaseModel): | ||
companies: List[Company] | ||
|
||
# Initialize the client with explicit API key | ||
sgai_client = Client(api_key="sgai-api-key") | ||
|
||
try: | ||
# SmartScraper request with infinite scroll | ||
response = sgai_client.smartscraper( | ||
website_url="https://www.ycombinator.com/companies?batch=Spring%202025", | ||
user_prompt="Extract all company names and their categories from the page", | ||
output_schema=CompaniesResponse, | ||
number_of_scrolls=10 # Scroll 10 times to load more companies | ||
) | ||
|
||
# Print the response | ||
print(f"Request ID: {response['request_id']}") | ||
|
||
# Parse and print the results in a structured way | ||
result = CompaniesResponse.model_validate(response['result']) | ||
print("\nExtracted Companies:") | ||
print("-" * 80) | ||
for company in result.companies: | ||
print(f"Name: {company.name}") | ||
print(f"Category: {company.category}") | ||
print(f"Location: {company.location}") | ||
print("-" * 80) | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
|
||
finally: | ||
sgai_client.close() |
This file contains hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try block should be a linear only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update it