11from typing import Any , Dict , List , cast , Union
2- from typing_extensions import NotRequired , TypedDict
2+ from typing_extensions import NotRequired , TypedDict , Optional
33from .request import Request , RequestConfig
44from .async_request import AsyncRequest , AsyncRequestConfig
55from ._config import ClientConfig
@@ -67,31 +67,8 @@ class BYOProxy(TypedDict):
6767 server : str
6868 auth : BYOProxyAuth
6969
70-
71- class AIScrapeParams (TypedDict ):
72- url : str
73- element_prompts : List [str ]
74- type : NotRequired [str ]
75- size_preset : NotRequired [str ]
76- is_mobile : NotRequired [bool ]
77- scale : NotRequired [int ]
78- width : NotRequired [int ]
79- height : NotRequired [int ]
80- force_rotate_proxy : NotRequired [bool ]
81- reject_request_pattern : NotRequired [List [str ]]
82- http_headers : NotRequired [object ]
83- goto_options : NotRequired [object ]
84- wait_for : NotRequired [object ]
85- cookies : NotRequired [object ]
86- page_position : NotRequired [int ]
87- root_element_selector : NotRequired [str ]
88- force_rotate_proxy : NotRequired [bool ]
89- byo_proxy : NotRequired [BYOProxy ]
90-
91-
92- class ScrapeParams (TypedDict ):
70+ class BaseAIScrapeParams (TypedDict ):
9371 url : str
94- elements : List [object ]
9572 advance_config : NotRequired [object ]
9673 size_preset : NotRequired [str ]
9774 is_mobile : NotRequired [bool ]
@@ -105,31 +82,30 @@ class ScrapeParams(TypedDict):
10582 wait_for : NotRequired [object ]
10683 cookies : NotRequired [object ]
10784
85+ class AIScrapeParamsWithPrompts (BaseAIScrapeParams ):
86+ selector : Optional [List [str ]]
87+ element_prompts : List [str ]
10888
109- class ScrapeResponse (TypedDict ):
110- success : bool
111- """
112- Indicates whether the translation was successful.
113- """
114- data : Any
89+ class AIScrapeParamsWithSelector (BaseAIScrapeParams ):
90+ selector : List [str ]
91+ element_prompts : Optional [List [str ]]
11592
93+ AIScrapeParams = Union [AIScrapeParamsWithSelector , AIScrapeParamsWithPrompts ]
11694
117- class LinkParams (TypedDict ):
118- text : str
119- href : str
95+ class LinkData (TypedDict ):
96+ type : str # "a" or "img"
97+ href : Optional [str ]
98+ text : Optional [str ]
12099
121100
122101class AIScrapeResponse (TypedDict ):
123102 success : bool
124- """
125- Indicates whether the translation was successful.
126- """
127- data : List [object ]
128- context : object
129- page_position_length : int
103+ data : List [Dict [str , Any ]]
104+ selectors : List [str ]
105+ context : Dict [str , List [str ]]
106+ link : List [LinkData ]
130107 page_position : int
131- selectors : object
132- link : List [LinkParams ]
108+ page_position_length : int
133109
134110
135111class Web (ClientConfig ):
@@ -154,7 +130,7 @@ def ai_scrape(self, params: AIScrapeParams) -> AIScrapeResponse:
154130 resp = Request (
155131 config = self .config ,
156132 path = path ,
157- params = cast (Dict [ Any , Any ] , params ),
133+ params = cast (AIScrapeParams , params ),
158134 verb = "post" ,
159135 ).perform_with_content ()
160136 return resp
@@ -228,15 +204,6 @@ async def ai_scrape(self, params: AIScrapeParams) -> AIScrapeResponse:
228204 ).perform_with_content ()
229205 return resp
230206
231- async def scrape (self , params : ScrapeParams ) -> ScrapeResponse :
232- path = "/web/scrape"
233- resp = await AsyncRequest (
234- config = self .config ,
235- path = path ,
236- params = cast (Dict [Any , Any ], params ),
237- verb = "post" ,
238- ).perform_with_content ()
239- return resp
240207
241208 async def html_to_any (self , params : HTMLToAnyParams ) -> Any :
242209 path = "/web/html_to_any"
0 commit comments