python3使用urllib示例取googletranslate(谷歌翻譯) -電腦資料

電腦資料 時(shí)間:2019-01-01 我要投稿
【clearvueentertainment.com - 電腦資料】

   

    復(fù)制代碼代碼如下:

    #!/usr/bin/env python3

    # -*- coding: utf-8 -*-

    # File Name : gt1.py

    # Purpose :

    # Creation Date : 1390366260

    # Last Modified : Wed 22 Jan 2014 06:14:11 PM CST

    # Release By : Doom.zhou

    import urllib.request

    import sys

    typ = sys.getfilesystemencoding()

    def translate(querystr, to_l="zh", from_l="en"):

    '''for google tranlate by doom

    '''

    C_agent = {'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.165063 Safari/537.36 AppEngine-Google."}

    flag = 'class="t0">'

    tarurl = "http://translate.google.com/m?hl=%s&sl=%s&q=%s \

    " % (to_l, from_l, querystr.replace(" ", "+"))

    request = urllib.request.Request(tarurl, headers=C_agent)

    page = str(urllib.request.urlopen(request).read().decode(typ))

    target = page[page.find(flag) + len(flag):]

    target = target.split("<")[0]

    return target

    print(translate("Hello world"))

   

您可能感興趣的文章:

python在命令行下使用google翻譯(帶語(yǔ)音)

python翻譯軟件實(shí)現(xiàn)代碼(使用google api完成)

python使用百度翻譯進(jìn)行中翻英示例

    QQ空間 搜狐微博 人人網(wǎng) 開(kāi)心網(wǎng) 百度搜藏更多

    Tags:python3 urllib

    復(fù)制鏈接收藏本文打印本文關(guān)閉本文返回首頁(yè)

    上一篇:pyside寫(xiě)ui界面入門(mén)示例

    下一篇:windows下wxPython開(kāi)發(fā)環(huán)境安裝與配置方法

   

相關(guān)文章

2009-08-08python 簡(jiǎn)易計(jì)算器程序,代碼就幾行

2014-02-02python3模擬百度登錄并實(shí)現(xiàn)百度貼吧簽到示例分享(百度貼吧自動(dòng)簽

2012-06-06Python查詢(xún)Mysql時(shí)返回字典結(jié)構(gòu)的代碼

2013-12-12python33 urllib2使用方法細(xì)節(jié)講解

2014-01-01python使用urllib模塊開(kāi)發(fā)的多線(xiàn)程豆瓣小站mp3下載器

2013-11-11python爬取網(wǎng)站數(shù)據(jù)保存使用的方法

2014-05-05從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(二)

2014-05-05python完成FizzBuzzWhizz問(wèn)題(拉勾網(wǎng)面試題)示例

2014-02-02python使用7z解壓軟件備份文件腳本分享

2014-06-06Python urlopen()函數(shù) 示例分享

   

文章評(píng)論

   

最 近 更 新

   

Python編寫(xiě)的com組件發(fā)生R6034錯(cuò)誤的原因

python實(shí)現(xiàn)圖片批量剪切示例

python使用cookielib庫(kù)示例分享

python 字典(dict)遍歷的四種方法性能測(cè)試

python實(shí)現(xiàn)監(jiān)控windows服務(wù)并自動(dòng)啟動(dòng)服務(wù)

python實(shí)現(xiàn)dnspod自動(dòng)更新dns解析的方法

python實(shí)現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)詳解

python二叉樹(shù)遍歷的實(shí)現(xiàn)方法

python的三目運(yùn)算符和not in運(yùn)算符使用示

python操作數(shù)據(jù)庫(kù)之sqlite3打開(kāi)數(shù)據(jù)庫(kù)、刪

   

熱 點(diǎn) 排 行

   

Python入門(mén)教程 超詳細(xì)1小時(shí)學(xué)會(huì)

python 中文亂碼問(wèn)題深入分析

比較詳細(xì)Python正則表達(dá)式操作指

Python字符串的encode與decode研

Python open讀寫(xiě)文件實(shí)現(xiàn)腳本

Python enumerate遍歷數(shù)組示例應(yīng)

Python 深入理解yield

Python+Django在windows下的開(kāi)發(fā)

python 文件和路徑操作函數(shù)小結(jié)

python 字符串split的用法分享

最新文章