Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Chinese in Alpine headless chrome?

I am using headless chrome based on alpine:3.7 image under docker. However, when I take a screenshot using Chrome DevTools Protocol, I found alpine cannot show languages other than English like the screenshot below.

I have tried to copy font to docker image

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

and tried to install font in alpine source:

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

But all methods failed. So my question is is Alpine support Chinese? And How can I use it using Docker?

python code to take the screenshot:

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

like image 358
einverne Avatar asked Mar 02 '18 10:03

einverne


1 Answers

Install wqy-zenhei package at testing channel will fix it.

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

like image 117
xiuqing pan Avatar answered Sep 30 '22 08:09

xiuqing pan