Neon-AI commited on
Commit
ddd0050
·
verified ·
1 Parent(s): e387557

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import FastAPI, HTTPException
2
  from playwright.sync_api import sync_playwright
3
  import os
 
 
4
 
5
  app = FastAPI(title="Neon Anime Scraper")
6
 
@@ -38,27 +40,27 @@ def search_anime(keyword: str):
38
  raise HTTPException(status_code=400, detail="Keyword is required")
39
 
40
  try:
41
- with sync_playwright() as p:
42
- browser = p.chromium.launch(headless=True)
43
- page = browser.new_page()
44
- url = f"https://hianime.to/search?keyword={keyword}"
45
- page.goto(url, wait_until="networkidle")
46
-
47
- results = page.evaluate("""
48
- () => {
49
- const items = Array.from(document.querySelectorAll(".flw-item"));
50
- return items.map(item => {
51
- const titleElem = item.querySelector(".film-name a");
52
- const thumbElem = item.querySelector("img");
53
- return {
54
- title: titleElem ? titleElem.textContent.trim() : null,
55
- url: titleElem ? titleElem.href : null,
56
- thumbnail: thumbElem ? thumbElem.src : null
57
- };
58
- });
59
- }
60
- """)
61
- browser.close()
62
 
63
  return {"keyword": keyword, "results": results}
64
 
 
1
  from fastapi import FastAPI, HTTPException
2
  from playwright.sync_api import sync_playwright
3
  import os
4
+ import requests
5
+ from bs4 import BeautifulSoup
6
 
7
  app = FastAPI(title="Neon Anime Scraper")
8
 
 
40
  raise HTTPException(status_code=400, detail="Keyword is required")
41
 
42
  try:
43
+ url = f"https://hianime.to/search?keyword={keyword}"
44
+ headers = {
45
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
46
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
47
+ "Chrome/120.0.0.0 Safari/537.36"
48
+ }
49
+ resp = requests.get(url, headers=headers, timeout=15)
50
+ if resp.status_code != 200:
51
+ raise HTTPException(status_code=resp.status_code, detail="Failed to fetch search page")
52
+
53
+ soup = BeautifulSoup(resp.text, "html.parser")
54
+ items = soup.select(".flw-item")
55
+ results = []
56
+ for item in items:
57
+ title_elem = item.select_one(".film-name a")
58
+ thumb_elem = item.select_one("img")
59
+ results.append({
60
+ "title": title_elem.text.strip() if title_elem else None,
61
+ "url": title_elem["href"] if title_elem else None,
62
+ "thumbnail": thumb_elem["src"] if thumb_elem else None
63
+ })
64
 
65
  return {"keyword": keyword, "results": results}
66