美文网首页千钻公会让前端飞工具癖
用Python3薅"百度翻译"的羊毛

用Python3薅"百度翻译"的羊毛

作者: zhaoolee | 来源:发表于2018-11-28 08:46 被阅读112次

薅"百度翻译"羊毛的起因 :

最近博主在https://unsplash.com搜索免费可商用图片的时候, 发现...

  • 搜索"苹果"
  • 搜索"apple"

如果我们能用Python3把"苹果"翻译成"apple", 然后用Python3爬虫批量获取图片就完美了

爬虫获取图片链接并不难, Unsplash本身提供了开源的接口(以apple为例): https://unsplash.com/napi/search/photos?query=apple

如何用Python3把"苹果"翻译成"Apple"?

  • 最好的方法是google在线翻译, 但国内很多地区上不了google, 而且google翻译接口名义上是收费的
  • 在国内推荐用百度, 好处一: 访问速度快; 好处二: 可以撸羊毛, 一个月可以免费翻译2000000个字符, 不用白不用

怎么用?

  • 然后百度会让你登录, 然后填一个申请成为百度开发者的表格, 填完后, 可以查看自己的开发者信息(需要填写表单, 填写的内容很简单, 下面是提交后, 展示信息的截图)

吐槽一下, 在python2即将被废弃的今天, 百度提供的示例Demo居然只有python2版本


  • 博主把百度的demo从python2版本转换为了python3版本, 转换后的代码如下:
import http.client
import hashlib
import urllib.parse
import random
import json


def baiduTranslate(q="苹果", fromLang="zh", toLang="en"):
    appid = '' #你的appid(这里是必填的, 从百度 开发者信息一览获取)
    secretKey = '' #你的密钥(这里是必填的, 从百度 开发者信息一览获取)

    httpClient = None
    myurl = '/api/trans/vip/translate'
    salt = random.randint(32768, 65536)
    sign = appid+q+str(salt)+secretKey
    m1 = hashlib.md5()
    m1.update(sign.encode())
    sign = m1.hexdigest()
    myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
    result = ""
    try:
        httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
        httpClient.request('GET', myurl)
        #response是HTTPResponse对象
        response = httpClient.getresponse()
        result = response.read()
    except Exception as e:
        print (e)
    finally:
        if httpClient:
            httpClient.close()

    return result


def main():
    print(json.loads(baiduTranslate(q="苹果")))

if __name__ == '__main__':
    main()

运行效果

  • 诗兴大发, 来两句诗
  • 黄河之水天上来, 奔流到海不复回
    The water of the Yellow River rises in the sky and runs to the sea and never returns.
  • 天若有情天亦老, 人间正道是沧桑
    If heaven is sentimental and heaven is old, the right way on earth is vicissitudes of life.

小结:

  • 虽然大家都喜欢黑百度, 但也离不开百度的产品, 就像博主,每隔三个月就要老老实实给百度网盘的超级会员续费, 否则就要失去5个T的空间
  • 既然百度给了大家一个薅羊毛的机会, 大家就用我提供的Python脚本来薅百度翻译的羊毛吧, 每月有2000000根羊毛可以薅呢~

相关文章

网友评论

  • 200ba7b7e6ac:免费的不用白不用
    zhaoolee:@貍桃仔 ◝:relieved:◟挖社会主义墙角,薅社会主义羊毛

本文标题:用Python3薅"百度翻译"的羊毛

本文链接:https://www.haomeiwen.com/subject/wjioqqtx.html