Hana Celeste commited on
Update app/fetch.py
Browse files- app/fetch.py +37 -79
app/fetch.py
CHANGED
|
@@ -1,95 +1,53 @@
|
|
| 1 |
-
import
|
| 2 |
-
import json
|
| 3 |
from fastapi import HTTPException
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
UAS = [
|
| 7 |
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 8 |
-
"(KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
| 9 |
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
|
| 10 |
-
"(KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
| 11 |
-
]
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"username": "scrapingant&proxy_type=residential&proxy_country=VN&browser=false",
|
| 16 |
-
"password": "fdd15bd8d6f042f893d8bb93a8e47722",
|
| 17 |
-
}
|
| 18 |
|
| 19 |
|
| 20 |
class Fetcher:
|
| 21 |
-
def __init__(self):
|
| 22 |
-
self.pw = None
|
| 23 |
-
self.browser = None
|
| 24 |
-
|
| 25 |
async def start(self):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
headless=True,
|
| 29 |
-
proxy=PROXY,
|
| 30 |
-
args=[
|
| 31 |
-
"--no-sandbox",
|
| 32 |
-
"--disable-dev-shm-usage",
|
| 33 |
-
"--disable-blink-features=AutomationControlled",
|
| 34 |
-
],
|
| 35 |
-
)
|
| 36 |
|
| 37 |
async def stop(self):
|
| 38 |
-
|
| 39 |
-
await self.browser.close()
|
| 40 |
-
if self.pw:
|
| 41 |
-
await self.pw.stop()
|
| 42 |
|
| 43 |
async def fetch(self, url: str):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
},
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
page = await context.new_page()
|
| 55 |
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if status >= 400:
|
| 78 |
-
raise HTTPException(status, "Upstream error")
|
| 79 |
-
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
except Exception:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return {
|
| 86 |
-
"status": status,
|
| 87 |
-
"data": data,
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
except TimeoutError:
|
| 91 |
-
raise HTTPException(504, "Timeout")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
await context.close()
|
|
|
|
| 1 |
+
import httpx
|
|
|
|
| 2 |
from fastapi import HTTPException
|
| 3 |
+
from urllib.parse import quote_plus
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
SCRAPINGANT_KEY = "fdd15bd8d6f042f893d8bb93a8e47722"
|
| 7 |
+
SCRAPINGANT_ENDPOINT = "https://api.scrapingant.com/v2/general"
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class Fetcher:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
async def start(self):
|
| 12 |
+
# không cần init gì
|
| 13 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
async def stop(self):
|
| 16 |
+
pass
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
async def fetch(self, url: str):
|
| 19 |
+
params = {
|
| 20 |
+
"url": url,
|
| 21 |
+
"x-api-key": SCRAPINGANT_KEY,
|
| 22 |
+
"proxy_type": "residential",
|
| 23 |
+
"proxy_country": "VN",
|
| 24 |
+
"browser": "false",
|
| 25 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
async with httpx.AsyncClient(timeout=20) as client:
|
| 29 |
+
resp = await client.get(
|
| 30 |
+
SCRAPINGANT_ENDPOINT,
|
| 31 |
+
params=params,
|
| 32 |
+
headers={
|
| 33 |
+
"Accept": "application/json, text/plain, */*",
|
| 34 |
+
"User-Agent": "Mozilla/5.0",
|
| 35 |
+
},
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if resp.status_code != 200:
|
| 39 |
+
raise HTTPException(
|
| 40 |
+
status_code=resp.status_code,
|
| 41 |
+
detail="Upstream error",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
text = resp.text.strip()
|
| 45 |
+
|
| 46 |
+
# ScrapingAnt trả JSON thẳng nếu API
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
+
return resp.json()
|
| 49 |
except Exception:
|
| 50 |
+
return {"raw": text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
except httpx.RequestError as e:
|
| 53 |
+
raise HTTPException(502, f"Proxy error: {str(e)}")
|
|
|