Python爬取有道詞典

from urllib import request,parseimport hashlibimport randomimport timeimport json#定義md5加密函數def getMD5(value):    aa = hashlib.md5()    aa.update(bytes(value,encoding="utf-8"))    sign = aa.hexdigest()    return sign#定義請求函數def fanyi(content):    base_url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'  #對salt和sign進行js解析    i = int(time.time() * 1000) + random.randint(0, 10)    value ="fanyideskweb" + content + str(i) + "aNPG!!u6sesA>hBAW1@(-"    data = {        'i': content,        'from': 'AUTO',        'to': 'AUTO',        'smartresult': 'dict',        'client': 'fanyideskweb',        'salt': i,        'sign': getMD5(value),        '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 - Language': 'zh - CN, zh;q = 0.9',        'Connection': 'keep - alive',        'Content - Type': 'application / x - www - form - urlencoded;charset = UTF - 8',        'X - Requested - With': 'XMLHttpRequest',        "Host": "fanyi.youdao.com",        "Origin": "http://fanyi.youdao.com",        "Referer": "http://fanyi.youdao.com/",        'Content-Length': len(data),        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36',        'Cookie': 'OUTFOX_SEARCH_USER_ID=168162067@218.241.251.155; OUTFOX_SEARCH_USER_ID_NCOO=2121343110.2274957; fanyi-ad-id=41685; fanyi-ad-closed=1; JSESSIONID=aaacmpXxOWbOKdy_3IIjw; ___rl__test__cookies=1522238432888'    }    res = request.Request(base_url,data=bytes(data,encoding="utf-8"),headers=headers)    response = request.urlopen(res)    html = response.read().decode('utf-8')    dict_data = json.loads(html)    dic_data = dict_data["smartResult"]    di_data = dic_data["entries"]    print(di_data)    for data in di_data:        if data != '\n':            print(data)if __name__ == '__main__':    while True:        content = input("請輸入您須要翻譯的內容:")        fanyi(content)        if content == "q":            break#好了執行一下看看吧
相關文章
相關標籤/搜索