-
Notifications
You must be signed in to change notification settings - Fork 101
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
[Feature Request] Add Async Implementation #48
Comments
Can you share the Intercom link? Thank you.
They should be able to use this directly this library if they want. They can do a regular HTTP get request against |
Here is the Intercom thread. |
Just sharing an example of making a 50 direct request to Ref: Code example: import aiohttp
import asyncio
import os
import json
import time
async def fetch_results(session, query):
params = {
'api_key': '...', # https://serpapi.com/manage-api-key
'engine': 'youtube',
'device': 'desktop',
'search_query': query,
'no_cache': 'true'
# additional params
}
url = 'https://serpapi.com/search.json'
async with session.get(url, params=params) as response:
results = await response.json()
data = []
if 'error' in results:
print(results['error'])
else:
for result in results.get('video_results', []):
data.append({
'title': result.get('title'),
'link': result.get('link'),
'channel': result.get('channel').get('name'),
})
return data
async def main():
# 50 queries
# here could be a dict or txt/csv/excel/json file
queries = [
'burly',
'creator',
'doubtful',
'chance',
'capable',
'window',
'dynamic',
'train',
'worry',
'useless',
'steady',
'thoughtful',
'matter',
'rotten',
'overflow',
'object',
'far-flung',
'gabby',
'tiresome',
'scatter',
'exclusive',
'wealth',
'yummy',
'play',
'saw',
'spiteful',
'perform',
'busy',
'hypnotic',
'sniff',
'early',
'mindless',
'airplane',
'distribution',
'ahead',
'good',
'squeeze',
'ship',
'excuse',
'chubby',
'smiling',
'wide',
'structure',
'wrap',
'point',
'file',
'sack',
'slope',
'therapeutic',
'disturbed'
]
data = []
async with aiohttp.ClientSession() as session:
tasks = []
for query in queries:
task = asyncio.ensure_future(fetch_results(session, query))
tasks.append(task)
start_time = time.time()
results = await asyncio.gather(*tasks)
end_time = time.time()
data = [item for sublist in results for item in sublist]
print(json.dumps(data, indent=2, ensure_ascii=False))
print(f'Script execution time: {end_time - start_time} seconds') # ~7.192448616027832 seconds
asyncio.run(main()) |
Hey Dmitriy, I have shared a similar code with the users that I wrote before:
|
@aliayar I saw 🙂 I've done a slightly different approach to possibly give some additional clarification for future users on how it could be done. I also wrote a blog post about it: Make Direct Async Requests to SerpApi with Python. I think the more examples we provide the better. The following part of this blog post will be about adding pagination with async functionality. |
One of our users asked for async requests implementation as in this example here.
https://www.twilio.com/blog/asynchronous-http-requests-in-python-with-aiohttp
It is possible that this function can accept a list of URLs to request as argument.
The text was updated successfully, but these errors were encountered: