| import asyncio | |
| from playwright.async_api import async_playwright | |
| async def scrape_tiktok_hashtags(niche): | |
| """ | |
| Uses Playwright to find trending hashtags for a niche on TikTok. | |
| Note: TikTok has heavy bot detection. This acts as a template for more complex scraping. | |
| """ | |
| async with async_playwright() as p: | |
| browser = await p.chromium.launch(headless=True) | |
| page = await browser.new_page() | |
| # Searching TikTok for hashtags | |
| search_url = f"https://www.tiktok.com/search/tag?q={niche}" | |
| await page.goto(search_url) | |
| await page.wait_for_timeout(5000) # Give it time to load | |
| # Extracting hashtag text | |
| tags = await page.query_selector_all('a[href*="/tag/"]') | |
| results = [] | |
| for tag in tags[:5]: | |
| text = await tag.inner_text() | |
| results.append(text) | |
| await browser.close() | |
| return results | |
| if __name__ == "__main__": | |
| niche = "fitness" | |
| loop = asyncio.get_event_loop() | |
| loop.run_until_complete(scrape_tiktok_hashtags(niche)) | |