Container commited on
Commit
dcf28a6
·
verified ·
1 Parent(s): d03a39f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
app.py CHANGED
@@ -17,41 +17,44 @@ def main():
17
  wait_time = 10
18
  header_list = '{"name": "John", "age": 30, "User-Agent": "New York"}'
19
 
20
- options = Options()
21
- options.add_argument('--headless')
22
-
23
- header_array = json.loads(header_list)
24
- for key, value in header_array.items():
25
- options.add_argument(f'--header="{key}: {value}"')
 
 
 
 
 
 
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
 
56
  if __name__ == '__main__':
57
  uvicorn.run(app='app:app', host="0.0.0.0", port=7860)
 
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
+ driver = webdriver.Chrome(options=options)
24
+
25
+ header_array = json.loads(header_list)
26
+ for key, value in header_array.items():
27
+ driver.add_header(str(key), str(value))
28
+
29
+ driver.get(target_url)
30
+
31
+ print(driver.page_source)
32
 
33
+ time.sleep(wait_time)
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ # 获取当前URL
36
+ current_url = driver.current_url
37
+
38
+ # 获取页面源代码
39
+ page_source = driver.page_source
40
+
41
+ # 获取cookie
42
+ cookies = driver.get_cookies()
 
 
 
 
43
 
44
+ # 是否有跳转过
45
+ is_jump = (target_url != current_url)
46
+
47
+ data = {
48
+ "url": current_url,
49
+ "page_source": page_source,
50
+ "cookies": cookies,
51
+ "is_jump": is_jump
52
+ }
53
+
54
+ driver.quit()
55
+ return {"code": 200,"data":data}
56
+ except Exception as e:
57
+ return {"code": 500,"msg":e}
58
 
59
  if __name__ == '__main__':
60
  uvicorn.run(app='app:app', host="0.0.0.0", port=7860)