Created
April 24, 2016 19:01
-
-
Save gregtap/ce9843317fa398c56fc389b81f3840f1 to your computer and use it in GitHub Desktop.
// fetch status tester Python
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
| import re | |
| import bs4 | |
| import aiohttp | |
| import asyncio | |
| HOST = 'http://dev-api.wyzant.com' | |
| # Holds fetched API links and their status. | |
| api_status = {} | |
| async def get_http_status(url): | |
| with aiohttp.Timeout(5): | |
| with aiohttp.ClientSession() as session: | |
| async with session.get(url) as resp: | |
| return resp.status | |
| async def extract_links_and_test_urls(): | |
| url = '{}/Help'.format(HOST) | |
| with aiohttp.ClientSession() as session: | |
| async with session.get(url) as resp: | |
| page = await resp.text() | |
| # Extract links. | |
| links_finder = bs4.SoupStrainer('a', href=re.compile('.*/Help/Api/.*')) | |
| links = bs4.BeautifulSoup(page, 'html.parser', parse_only=links_finder) | |
| # Build a list of links | |
| links = ['{}{}'.format(HOST, link['href']) for link in links] | |
| # Get HTTP status for each link. | |
| for link in links[:100]: | |
| api_status[link] = await get_http_status(link) | |
| if __name__ == '__main__': | |
| loop = asyncio.get_event_loop() | |
| conn = aiohttp.connector.TCPConnector() | |
| loop.run_until_complete(extract_links_and_test_urls()) | |
| print(api_status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment