如何在Alpine无头镀铬中使用中文?

时间:2018-03-02 10:50:23

标签: docker fonts dockerfile alpine

我正在使用基于高山的无头镀铬:docker下的3.7图像。但是,当我使用Chrome DevTools协议截取屏幕截图时,我发现alpine无法显示除英语之外的其他语言,如下面的屏幕截图所示。

我试图将字体复制到docker image

COPY NotoSansCJKsc-Regular.otf /usr/share/fonts/

并尝试在alpine源中安装字体:

RUN apk add --no-cache bash \
ttf-ubuntu-font-family \
font-adobe-100dpi \
font-noto \
ttf-dejavu \

但所有方法都失败了。所以我的问题是阿尔卑斯支持中文?我如何使用Docker使用它?

截取屏幕截图的python代码:

import threading

import pychrome
import time
import base64


class EventHandler(object):
    screen_lock = threading.Lock()

    def __init__(self, browser, tab):
        self.browser = browser
        self.tab = tab
        self.start_frame = None
        self.html_content = None
        self.is_first_request = True

    def frame_start_loading(self, frameId):
        if not self.start_frame:
            self.start_frame = frameId

    def request_intercepted(self, interceptionId, request, **kwargs):
        if self.is_first_request:
            self.is_first_request = False
            headers = request.get('headers', {})
            self.tab.Network.continueInterceptedRequest(
                interceptionId=interceptionId,
                headers=headers,
                method='POST',
                postData="hello post data: %s" % time.time()
            )
        else:
            self.tab.Network.continueInterceptedRequest(
                interceptionId=interceptionId
            )

    def frame_stop_loading(self, frameId):
        if self.start_frame == frameId:
            self.tab.Page.stopLoading()
            result = self.tab.Runtime.evaluate(expression="document.documentElement.outerHTML")
            self.html_content = result.get('result', {}).get('value', "")
            print self.html_content
            self.tab.stop()


def close_all_tabs(browser):
    if len(browser.list_tab()) == 0:
        return
    for tab in browser.list_tab():
        try:
            tab.stop()
        except pychrome.RuntimeException:
            pass

        browser.close_tab(tab)
    assert len(browser.list_tab()) == 0

if __name__ == '__main__':
    # create a browser instance
    browser = pychrome.Browser(url="http://127.0.0.1:9222")

    print browser.version()

    # create a tab
    tab = browser.new_tab()

    # register callback
    def request_will_be_sent(**kwargs):
        print("[start] start loading: %s" % kwargs.get('request').get('url'))

    tab.set_listener("Network.requestWillBeSent", request_will_be_sent)

    def received_callback(**kwargs):
        print "[received] response type %s" % kwargs.get('type', '')
        resp = kwargs.get('response', {})
        resp_status = resp.get('status', '')
        resp_headers = resp.get('headersText')
        print "response status %s %s" % (resp_status, resp_headers)

    tab.set_listener("Network.responseReceived", received_callback)

    def frame_stop_loading():
        print "frame stop loading"

    tab.set_listener("Page.frameStoppedLoading", frame_stop_loading)

    def loading_finished(**kwargs):
        print "[loading finished]"

    # when HTTP request has finished loading
    tab.set_listener("Network.loadingFinished", loading_finished)

    # start the tab
    tab.start()

    net = tab.call_method("Network.enable")
    navi = tab.call_method("Page.navigate", url="https://www.douban.com", _timeout=5)
    tab.wait(5)

    screen_data = tab.call_method("Page.captureScreenshot")
    image_data = screen_data.get('data', '')

    with open("image.png", "wb") as file:
        file.write(image_data.decode('base64'))

    html = tab.Runtime.evaluate(expression="document.documentElement.outerHTML")
    print html['result']['value']

    all_cookies = tab.Network.getAllCookies()
    print all_cookies

    tab.stop()

    browser.close_tab(tab)

error chinese page

1 个答案:

答案 0 :(得分:3)

在测试频道安装wqy-zenhei包将修复它。

echo @edge http://nl.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && apk add wqy-zenhei@edge