python Spider Man(爬虫侠)二,之百度翻译小程序

from urllib import request,parse
import  json
def fanyi(msg):
        data = {
                'kw':content
        }
        data = parse.urlencode(data)#把字典改成kw=content 这种
        basr_url = 'http://fanyi.baidu.com/sug'

        headers = {
                "Content-Length": len(data),  # 动态计算data长度 根据请求头的需要
                "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
        }
        req= request.Request(url=basr_url,data=bytes(data,encoding='utf-8'),headers=headers)
        resp = request.urlopen(req).read()
        html =resp.decode('utf-8')
        json_Data = json.loads(html)
       print(json_Data)
   for itm in json_Data['data']:
                print(itm['k'],itm['v'])
if __name__ == '__main__':
        content = input('请输入您要翻译的内容:')

        fanyi(content)
python Spider Man(爬虫侠)二,之百度翻译小程序_第1张图片

 

你可能感兴趣的:(python3爬虫学习路上)