簡體   English   中英

適用於GAE的Python無頭瀏覽器

[英]Python Headless Browser for GAE

我正在嘗試在Google Appengine上使用Angular.js客戶端和webapp2。

為了解決SEO問題,我們的想法是使用無頭瀏覽器來運行javascript服務器端,並將生成的html提供給爬蟲。

在谷歌應用引擎上運行的python是否有無頭瀏覽器?

這是一個超級元想法。 Web服務器使用無頭Web瀏覽器實現Web請求以呈現頁面並返回結果。 唷。

在無頭瀏覽器上看一下以下答案,特別注意基於Python的瀏覽器。

無頭瀏覽器問題: 無頭互聯網瀏覽器?

看起來支持Javascript的都使用WebKit並需要PyQt或Pyside。 這意味着由於存在運行時限制,您無法在App Engine上運行它們。

我會建議您進行搜索引擎優化目的,您可以使用Jinja2模板或其他東西進行某種用戶代理檢測並發出超級縮小的頁面版本。 無論如何,你可能會以這種方式獲得更好的表現。

現在可以在帶有自定義運行時的App Engine Flex上完成,所以我添加了這個答案,因為這個問題是谷歌彈出的第一件事。

我將這個自定義運行時基於我使用預構建的python運行時的其他GAE flex微服務

項目結構:

webdrivers/
- geckodriver
app.yaml
Dockerfile
main.py
requirements.txt

app.yaml中:

service: my-app-engine-service-name
runtime: custom
env: flex
entrypoint: gunicorn -b :$PORT main:app --timeout 180

Dockerfile:

FROM gcr.io/google-appengine/python
RUN apt-get update
RUN apt-get install -y xvfb
RUN apt-get install -y firefox
LABEL python_version=python
RUN virtualenv --no-download /env -p python
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
CMD exec gunicorn -b :$PORT main:app --timeout 180

requirements.txt:

Flask==0.12.2
gunicorn==19.7.1
selenium==3.13.0
pyvirtualdisplay==0.2.1

main.py

import os
import traceback

from flask import Flask, jsonify, Response
from selenium import webdriver
from pyvirtualdisplay import Display

app = Flask(__name__)

# Add the webdrivers to the path
os.environ['PATH'] += ':'+os.path.dirname(os.path.realpath(__file__))+"/webdrivers"

@app.route('/')
def hello():
    return 'Hello!!'

@app.route('/test/', methods=['GET'])
def go_headless():
    try:
        display = Display(visible=0, size=(1024, 768))
        display.start()
        d = webdriver.Firefox()
        d.get("http://www.python.org")    
        page_source = d.page_source.encode("utf-8")
        d.close()
        display.stop()
        return jsonify({'success': True, "result": page_source[:500]})
    except Exception as e:
        print traceback.format_exc()
        return jsonify({'success': False, 'msg': str(e)})

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

從這里下載geckodriver(linux 64):

https://github.com/mozilla/geckodriver/releases

其他說明:

  • 請注意您使用的geckodriver,firefox和selenium的版本,因為它可能是finnickey,給出此錯誤WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmp 48P If you specified a log_file in the FirefoxBinary constructor, check it for details. WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmp 48P If you specified a log_file in the FirefoxBinary constructor, check it for details.
  • 除非你使用的是傳統geckodriver / firefox,否則不要設置DesiredCapabilities().FIREFOX["marionette"] = False https://github.com/SeleniumHQ/selenium/issues/5106
  • display = Display(visible=0, size=(1024, 768))需要修復此錯誤: 如何修復Selenium WebDriverException:在我們連接之前,瀏覽器似乎已經退出了?

要在本地測試:

docker build . -t my-docker-image-tag
docker run -p 8080:8080 --name=my-docker-container-name my-docker-image-tag

要部署到應用引擎:

gcloud app deploy app.yaml --version dev --project my-app-engine-project-id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM