【发布时间】:2021-12-19 07:22:57
【问题描述】:
我正在尝试将一个 webscraper - python Selenium 在一个 docker 容器中部署到我可以请求的数字海洋。从不同的网站发布一个 url。使用数字海洋上的控制台,我可以运行代码并且运行良好。所以我认为问题在于我如何接收或发布网址到网络抓取工具。目前它正在返回一个<Response [422]>
这里是代码,我向 extract_text_via_scraper_service 函数传递了一个字符串形式的 url,例如“https://google.com”,docker应用应该以字典的形式返回标题:
SCRAPER_API_TOKEN_HEADER=os.environ.get("SCRAPER_API_TOKEN_HEADER")
SCRAPER_API_ENDPOINT=os.environ.get("SCRAPER_API_ENDPOINT")
def extract_text_via_scraper_service(website): # website = url
answer = {}
if SCRAPER_API_ENDPOINT is None:
return answer
if SCRAPER_API_TOKEN_HEADER is None:
return answer
if website is None:
return answer
# send url through HTTP POST
# return dict {}
headers={
"Authorization": f"Bearer {SCRAPER_API_TOKEN_HEADER}"
}
r = requests.post(SCRAPER_API_ENDPOINT, data=website, headers=headers)
print(r)
if r.status_code in range(200, 299):
if r.headers.get("content-type") == 'application/json':
answer = r.json()
return answer
码头文件:
import pathlib
import os
import io
from functools import lru_cache
from fastapi import (
FastAPI,
Header,
HTTPException,
Depends,
Request,
)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementNotInteractableException, NoSuchElementException, StaleElementReferenceException, TimeoutException, ElementClickInterceptedException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from pydantic import BaseSettings
class Settings(BaseSettings):
app_auth_token: str
debug: bool = False
echo_active: bool = False
app_auth_token_prod: str = None
skip_auth: bool = False
class Config:
env_file = ".env"
@lru_cache
def get_settings():
return Settings()
settings = get_settings()
DEBUG=settings.debug
BASE_DIR = pathlib.Path(__file__).parent
UPLOAD_DIR = BASE_DIR / "uploads"
app = FastAPI()
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
# REST API
@app.get("/", response_class=HTMLResponse) # http GET -> JSON
def home_view(request: Request, settings:Settings = Depends(get_settings)):
return templates.TemplateResponse("home.html", {"request": request, "abc": 123})
def verify_auth(authorization = Header(None), settings:Settings = Depends(get_settings)):
if settings.debug and settings.skip_auth:
return
if authorization is None:
raise HTTPException(detail="Invalid endpoint", status_code=401)
label, token = authorization.split()
if token != settings.app_auth_token:
raise HTTPException(detail="Invalid endpoint", status_code=401)
@app.post("/") # http POST
async def prediction_view(website, authorization = Header(None), settings:Settings = Depends(get_settings)):
verify_auth(authorization, settings)
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome("/usr/local/bin/chromedriver", options=options)
wait = WebDriverWait(driver, 10)
driver.get(website)
title = "Sorry, we failed to get the correct name"
#title
try:
title = driver.find_element(By.XPATH, "//title")
title = title.get_attribute("innerText")
except:
pass
print(title)
return{"results": title, "original": title}
任何帮助表示赞赏。
【问题讨论】:
-
它确认这不是授权问题。所以我怀疑这个问题与我如何发布/接收网址有关,它似乎不理解数据类型,但我只是尝试将 docker 参数更改为 website:str ,但不幸的是它仍然返回同样的问题。
-
r.text返回什么? -
r.text 返回
{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}。我想我需要先以某种方式对 url 进行编码。
标签: python docker python-requests digital-ocean