from selenium import webdriver
from fastapi import FastAPI, Query
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
app = FastAPI()
@app.get('/gethtml')
async def user(
*,
url: str = Query(..., title="need url")
):
# url = 'http://www.k3.cn/p/oadiuafpeui.html?show=3&type=image&source_id=86722&source_type=ad'
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('disable-gpu')
options.add_argument('no-sandbox')
# browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver")
browser = webdriver.Chrome(chrome_options=options)
browser.get(url)
# 显示等待
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, 'productmemo'))) # 等到id为content_left的元素加载完毕,最多等10秒
item = browser.find_element_by_tag_name('html')
html = item.get_attribute('innerHTML')
browser.quit()
return { 'html': html}