Google翻译API

Github上面其实有Google翻译的API,由于我国社会主义初级阶段的基本国情,直接拿来用好像行不通!

import requests
from bs4 import BeautifulSoup
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        return r.text
    except:
        print("Get HTML Text Failed!")
        return 0
def google_translate(to_translate, from_language="en", to_language="ch-CN"):
    #根据参数生产提交的网址
    base_url = "https://translate.google.cn/m?hl={}&sl={}&ie=UTF-8&q={}"
    url = base_url.format(to_language, from_language, to_translate)
    #获取网页
    html = getHTMLText(url)
    if html:
        soup = BeautifulSoup(html, "html.parser")
    #解析网页得到翻译结果
    try:
        result = soup.find_all("div", {"class":"t0"})[0].text
    except:
        print("Translation Failed!")
        result = ""
    return result
print(google_translate("Hello World!"))
#你好,世界

代码好像超级简单哈,有空的话我想把它做成一个库。有github的给个星星呗,鼓励一下!
https://github.com/wen-chen/PyTransl

Chrome保存网页为图片

Google Chrome浏览器,可以很方便地将网页转换为PDF。右击网页空白处,在弹出的快捷菜单中选择”打印”,在”打印选项卡”中选择”另存为PDF”。
Chrome_tips_1
有时候网页太长,保存为PDF,分页显示很难看