python3.0学習6:遅延でWebページにアクセス

2949 ワード

'''   :python         ,         。   :                         ,         ,           '''

import urllib.request
#parse  
import urllib.parse
import json
import time #      

while True: #       
    content=input("          (  q    ):")
    if content=='q':#       ,      
         break
    url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'

    data={}
    data['type']='AUTO'

    data['i']=content

    data['doctype']='json'
    data['xmlVersion']='1.8'
    data['keyfrom']='fanyi.web'
    data['ue']='UTF-8'
    data['action']='FU_BY_ENTER'
    data['typoResult']='ture'

    data=urllib.parse.urlencode(data).encode('utf-8')

    req=urllib.request.Request(url,data)

    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36')


    response=urllib.request.urlopen(req)
    html=response.read().decode('utf-8')

    target=json.loads(html)


    print("       : %s" %target['smartResult']['entries'][1])
    time.sleep(5)


#      
============== RESTART: C:\Users\Administrator\Desktop
iub.py ============== ( q ): : hello;hi ( q ):happy : adj. ; ; ( q ):q >>>