File size: 1,900 Bytes
3b2ecd2
494cdcf
973d2bc
 
ca11d6d
baf3cbb
02fa17e
e37298f
973d2bc
e37298f
973d2bc
 
65230b0
 
 
3895bea
8996078
5430983
6d15222
5430983
3895bea
65230b0
 
 
 
02fa17e
 
 
3895bea
8996078
65230b0
 
 
 
6fd44c2
3895bea
65230b0
3895bea
 
 
 
 
 
 
 
 
 
 
 
 
8996078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e37298f
973d2bc
 
 
e37298f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from seleniumwire import webdriver
from selenium.webdriver.chrome.options import Options
from fastapi import FastAPI, Request
import uvicorn
import time
import json
from urllib.parse import unquote

app = FastAPI()

@app.get("/")
def main():
    return {"code": 200,"msg":"Success"}
    
@app.get("/chrome")
def chrome(url:str=None,wait:int=5,header:str=None,cookie:str=None):
    
    target_url = url
    wait_time = wait
    header_list = header
    cookie_string = cookie

    if target_url == None:
        return {"code": 500,"msg":"No target URL"}

    if wait_time not in range(0, 31):
        return {"code": 500,"msg":"The waiting time must be between 0 and 30"}

    header_array = {}
    
    try:
        if header_list == None:
            pass
        else:
            # print(unquote(header_list))
            header_array.update(json.loads(unquote(header_list)))
    except Exception as e:
        return {"code": 500,"msg":"The header field is not JSON"}

    if cookie_string == None:
        pass
    else:
        header_array.update({"cookie":cookie_string})
            
    options = Options()
    options.add_argument('--headless')
    
    driver = webdriver.Chrome(options=options)

    driver.header_overrides = header_array
    
    driver.get(target_url)

    print(driver.page_source)
    
    time.sleep(wait_time)

    # 获取当前URL
    current_url = driver.current_url
    
    # 获取页面源代码
    page_source = driver.page_source
    
    # 获取cookie
    cookies = driver.get_cookies()

    # 是否有跳转过
    is_jump = (target_url != current_url)
    
    data = {
        "url": current_url,
        "page_source": page_source,
        "cookies": cookies,
        "is_jump": is_jump
    }
    
    driver.quit()
    return {"code": 200,"data":data}

if __name__ == '__main__':
    uvicorn.run(app='app:app', host="0.0.0.0", port=7860)