繁体   English   中英

Python 中的 Selenium + Flask/Falcon - 502 错误网关错误

[英]Selenium + Flask/Falcon in Python - 502 Bad Gateway Error

我正在使用 selenium 在使用Flask for Python 的 API 端点内对网站进行无头抓取。 我做了几个测试,我的 selenium 抓取代码在脚本中完美运行,同时在本地主机中作为 API 运行。 但是,当我在远程服务器中部署代码时,请求总是返回502 Bad Gateway error 这很奇怪,因为通过记录我可以看到抓取工作正常,但服务器在抓取完成处理之前以 502 响应,就好像它试图设置代理但失败了。 我还注意到,在我的代码中删除time.sleep使其返回 200,尽管结果可能是错误的,因为它没有给 selenium 适当的时间来加载所有页面以进行抓取。

我还尝试设置使用 falcon 而不是烧瓶,但出现了类似的错误。 这是我最近使用Falcon 的代码示例:

class GetUrl(object):

    def on_get(self, req, resp):
        """
        Get Request
        :param req:
        :param resp:
        :return:
        """

        # read parameter
        req_body = req.bounded_stream.read()
        json_data = json.loads(req_body.decode('utf8'))
        url = json_data.get("url")

        # get the url
        options = Options()
        options.add_argument("--headless")
        driver = webdriver.Firefox(firefox_options=options)

        driver.get(url)
        time.sleep(5)
        result = False

        # check for outbound links
        content = driver.find_elements_by_xpath("//a[@class='_52c6']")
        if len(content) > 0:
            href = content[0].get_attribute("href")
            result = True

        driver.quit()

        # make the return
        return_doc = {"result": result}
        resp.body = json.dumps(return_doc, sort_keys=True, indent=2)
        resp.content_type = 'text/string'
        resp.append_header('Access-Control-Allow-Origin', "*")
        resp.status = falcon.HTTP_200

我看到一些其他类似的问题,像这样的,但即使我可以看到,有一个gunicorn在我的服务器上运行,我没有nginx的,或至少它不运行它应该运行。 而且我认为 Falcon 不会使用它。 那么,我到底做错了什么? 非常感谢这个问题中的一些光,谢谢!

这可能有效:

from IPython.display import clear_output
import time as time
import json
!apt-get update
!apt install chromium-chromedriver
!which chromedriver
!pip install selenium
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
!pip install page_objects
import page_objects
from page_objects import PageObject, PageElement
time.sleep(1)
clear_output()

class GetUrl(object):

    def on_get(self, req, resp):
        """
        Get Request
        :param req:
        :param resp:
        :return:
        """

        # read parameter
        req_body = req.bounded_stream.read()
        json_data = json.loads(req_body.decode('utf8'))
        url = json_data.get("https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175")

        # get the url
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        driver = webdriver.Chrome('chromedriver',options = options)
        driver.implicitly_wait(3)

        driver.get("https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175")
        result = False

        # check for outbound links
        contentStorage = []
        content = driver.find_elements_by_tag_name('a')
        for i in content:
            contentStorage.append(i.get_attribute('text'))
            result = True

        #driver.quit()

        # make the return
        return_doc = {"result": result}
        resp.body = json.dumps(return_doc, sort_keys=True, indent=2)
        resp.content_type = 'text/string'
        resp.append_header('Access-Control-Allow-Origin', "*")
        resp.status = falcon.HTTP_200

但是,我在不使用类对象的情况下对其进行了测试,并且它使用的是 Chrome 而不是 FireFox:

from IPython.display import clear_output
import time as time
!apt-get update
!apt install chromium-chromedriver
!which chromedriver
!pip install selenium
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
!pip install page_objects
import page_objects
from page_objects import PageObject, PageElement
time.sleep(1)
clear_output()
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options = options)
driver.implicitly_wait(3)
driver.get('https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175')
content = driver.find_elements_by_tag_name('a')
contentStorage = []
for i in content:
  contentStorage.append(i.get_attribute('text'))
#driver.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM