Container commited on
Commit
8996078
·
verified ·
1 Parent(s): 06ff70d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -1,9 +1,5 @@
1
  from selenium import webdriver
2
  from selenium.webdriver.chrome.options import Options
3
- from selenium.webdriver.common.by import By
4
- from selenium.webdriver.support.ui import WebDriverWait
5
- from selenium.webdriver.support import expected_conditions as EC
6
- from selenium.common.exceptions import WebDriverException, TimeoutException
7
  from fastapi import FastAPI, Request
8
  import uvicorn
9
  import time
@@ -11,49 +7,57 @@ import json
11
 
12
  app = FastAPI()
13
 
 
 
 
 
14
  @app.get("/")
15
  def main():
16
  target_url = 'https://test5.container-z.art/test26'
17
  wait_time = 10
18
- header_list = '{"name": "John", "age": 30, "User-Agent": "New York"}'
19
-
20
- try:
21
- options = Options()
22
- options.add_argument('--headless')
23
-
24
- header_array = json.loads(header_list)
25
-
26
-
27
- driver = webdriver.Chrome(options=options)
28
- driver.get(target_url)
29
-
30
- print(driver.page_source)
31
-
32
- time.sleep(wait_time)
33
-
34
- # 获取当前URL
35
- current_url = driver.current_url
36
-
37
- # 获取页面源代码
38
- page_source = driver.page_source
39
-
40
- # 获取cookie
41
- cookies = driver.get_cookies()
42
-
43
- # 是否有跳转过
44
- is_jump = (target_url != current_url)
45
-
46
- data = {
47
- "url": current_url,
48
- "page_source": page_source,
49
- "cookies": cookies,
50
- "is_jump": is_jump
51
- }
52
-
53
- driver.quit()
54
- return {"code": 200,"data":data}
55
- except Exception as e:
56
- return {"code": 500,"msg":e}
 
 
 
 
57
 
58
  if __name__ == '__main__':
59
  uvicorn.run(app='app:app', host="0.0.0.0", port=7860)
 
1
  from selenium import webdriver
2
  from selenium.webdriver.chrome.options import Options
 
 
 
 
3
  from fastapi import FastAPI, Request
4
  import uvicorn
5
  import time
 
7
 
8
  app = FastAPI()
9
 
10
+ def convert_cookies_to_dict(cookies):
11
+ cookies = dict([l.split("=", 1) for l in cookies.split("; ")])
12
+ return cookies
13
+
14
  @app.get("/")
15
  def main():
16
  target_url = 'https://test5.container-z.art/test26'
17
  wait_time = 10
18
+ header_list = '{"name": "John", "age": 30, "user-agent": "New York"}'
19
+ mode = "get"
20
+ cookies_string = "_zap=386b2646-cd4f-48b0-831c-0f4e0fcb84d; d_c0=ATBVpddW2hePTs-uWJUqnuikOgCLTtzDaQ=|1702569488; _xsrf=9Exea3EejXTsAZewjRCns4TPklsjdCpV; __zse_ck=001_xCDDL0hUsjpAaMgmQE7pCfeU+zXi0/T+YTGbyY=OkkWy54lHQjmO1If3BOBMUzDjeY=d26WyAD5j0BG=m7jR7RrpRqv2ImjXDfJPzUEzARb4/TImweWRybZ5aGbVke4B; BEC=9f6899d2c0337126a73fd6e01ed7c740; gdxidpyhxdE=tfBS35OO12TxVYkb6i%5Ce7mQekmAMzv2e1Ah%5CGS4h%5CwU7xS47p9%2Fd4T%5CNEmx7s%2Bu%2Fr8mkLktUMVIPQ404Z1OxrejYyRQ3aSgLL7CBsT2nRhTXpzWaxqr7KyuaHSCMZjsK6OBaCM5GsMTSor4baQvmzWfo5SZqccCgWZTrn4Ii2tgDNH0w%3A1718799277544"
21
+
22
+ header_array = json.loads(header_list)
23
+ cookies = convert_cookies_to_dict(cookies_string)
24
+
25
+ options = Options()
26
+ options.add_argument('--headless')
27
+ for key, value in header_array.items():
28
+ options.add_argument(f'{key}="{value}"')
29
+
30
+ driver = webdriver.Chrome(options=options)
31
+ # print(cookies)
32
+ # driver.add_cookie(cookies)
33
+
34
+ driver.get(target_url)
35
+
36
+ print(driver.page_source)
37
+
38
+ time.sleep(wait_time)
39
+
40
+ # 获取当前URL
41
+ current_url = driver.current_url
42
+
43
+ # 获取页面源代码
44
+ page_source = driver.page_source
45
+
46
+ # 获取cookie
47
+ cookies = driver.get_cookies()
48
+
49
+ # 是否有跳转过
50
+ is_jump = (target_url != current_url)
51
+
52
+ data = {
53
+ "url": current_url,
54
+ "page_source": page_source,
55
+ "cookies": cookies,
56
+ "is_jump": is_jump
57
+ }
58
+
59
+ driver.quit()
60
+ return {"code": 200,"data":data}
61
 
62
  if __name__ == '__main__':
63
  uvicorn.run(app='app:app', host="0.0.0.0", port=7860)