Python3-抓取某翻译网页 获取js数据

#抓取有道翻译网页 获取js数据

#找接口,至少尝试三次以上

from urllib import parse,request
import time,random
import hashlib
import json

#md5加密
def getMd5(str):
    md5 = hashlib.md5()
    md5.update(bytes(str, encoding='utf-8'))  # 加密类型是字节类型
    sign = md5.hexdigest()  # 加密
    print(sign)
    return sign

def fanyi(key):

    base_url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

    #生成salt  JS返回的是毫秒级时间戳、python拿到的是秒级的时间戳
    # i="" + ((new Date).getTime() + parseInt(10 * Math.random(), 10))#js格式
    salt=""+str(int(time.time()*100)+random.randint(0,10))
    #生成sign
    # o = ("fanyideskweb" + t + i + "ebSeFb%=XZ%T[KZ)c(sy!");
    # t='job'
    o_str="fanyideskweb" + key + salt + "ebSeFb%=XZ%T[KZ)c(sy!"
    sign=getMd5(o_str)


    data={
        'i':key,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': salt,
    'sign': sign,
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTIME',
    'typoResult': 'false'
    }
    data=parse.urlencode(data)

    headers={
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    # 'Accept-Encoding: gzip, deflate
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'OUTFOX_SEARCH_USER_ID=-2024838773@10.169.0.83; JSESSIONID=aaaln4v6n93KX5B3rc_kw; OUTFOX_SEARCH_USER_ID_NCOO=807868141.5217086; ___rl__test__cookies=1523607822055',
    'Host': 'fanyi.youdao.com',
    'Origin': 'http://fanyi.youdao.com',
    'Referer':' http://fanyi.youdao.com/',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    ,'X-Requested-With': 'XMLHttpRequest',
        'Content-Length':len(data)

    }

    req=request.Request(base_url,bytes(data,encoding='utf-8'),headers=headers)
    response=request.urlopen(req)
    content=response.read()
    content=content.decode('utf-8')
    print(content)

    # 整理代码
    data_json=json.loads(content)
    smart_json=data_json['smartResult']
    smart_item=smart_json['entries']
    for item in smart_item:
        print(item)

    '''
    {"translateResult": [[{"tgt": "hadoop", "src": "hadoop"}]], 
     "errorCode": 0, "type": "en2zh-CHS",
     "smartResult": {"entries": ["", "分布式计算\r\n"], "type": 1}}
    '''

if __name__=='__main__':
    while True:

        content=input('请输入要翻译的内容:')
        fanyi(content)
        if content=='q':
            break

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/apple/PycharmProjects/stage4/spider/2018_03_06/01spider_js.py
请输入要翻译的内容:work
c6338d04dd302e3f7717ad16167a8cd3
{"translateResult":[[{"tgt":"工作","src":"work"}]],"errorCode":0,"type":"en2zh-CHS","smartResult":{"entries":["","n. 工作;[物] 功;产品;操作;职业;行为;事业;工厂;著作;文学、音乐或艺术作品\r\n","vt. 使工作;操作;经营;使缓慢前进\r\n","vi. 工作;运作;起作用\r\n"],"type":1}}


n. 工作;[物] 功;产品;操作;职业;行为;事业;工厂;著作;文学、音乐或艺术作品


vt. 使工作;操作;经营;使缓慢前进


vi. 工作;运作;起作用


请输入要翻译的内容:


阅读更多
版权声明: https://blog.csdn.net/zbrj12345/article/details/79981542
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭