• Python修改headers参数的两种方法


    1.通过Request的headers参数修改

     1 import urllib.request
     2 import urllib.parse
     3 import json
     4 
     5 content = input("请输入翻译的内容:")
     6 
     7 url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
     8 
     9 head = {}
    10 head['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
    11 data = {}
    12 
    13 data['i'] = content
    14 
    15 data['from'] = 'en'
    16 data['to'] = 'zh-CHS'
    17 data['smartresult'] = 'dict'
    18 data['client'] = 'fanyideskweb'
    19 data['salt'] = '15934837793668'
    20 data['sign'] = '6c263b05b4511c7c9fc9e540d8cb3b42'
    21 data['ts'] = '1593483779366'
    22 data['bv'] = 'dd0840fad0d96c2e9de5a4f181a39d98'
    23 data['doctype'] = 'json'
    24 data['version'] = '2.1'
    25 data['keyfrom'] = 'fanyi.web'
    26 data['action'] = 'FY_BY_CLICKBUTTION'
    27 data = urllib.parse.urlencode(data).encode('utf-8')
    28 
    29 req = urllib.request.Request(url,data,head)
    30 response = urllib.request.urlopen(req)
    31 
    32 html =response.read().decode('utf-8')
    33 
    34 target = json.loads(html)
    35 print("翻译的结果是:%s" %(target['translateResult'][0][0]['tgt']))

    运行结果

    输入翻译的内容:love
    翻译的结果是:爱
    >>> req.headers
    {'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}

    2.通过Request.add_header()方法修改

    附加代码

    import urllib.request
    import urllib.parse
    import json
    
    content = input("请输入翻译的内容:")
    
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    
    #head = {}
    #head['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'
    data = {}
    
    data['i'] = content
    
    data['from'] = 'en'
    data['to'] = 'zh-CHS'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['salt'] = '15934837793668'
    data['sign'] = '6c263b05b4511c7c9fc9e540d8cb3b42'
    data['ts'] = '1593483779366'
    data['bv'] = 'dd0840fad0d96c2e9de5a4f181a39d98'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_CLICKBUTTION'
    data = urllib.parse.urlencode(data).encode('utf-8')
    
    req = urllib.request.Request(url,data)
    req.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36')
    response = urllib.request.urlopen(req)
    
    html =response.read().decode('utf-8')
    
    target = json.loads(html)
    print("翻译的结果是:%s" %(target['translateResult'][0][0]['tgt']))
  • 相关阅读:
    甩掉DataList,Repeater,列表数据显示得灵活
    拟将《汉字速查》更名为《汉文博士》,诸位有何高见?
    新一版的汉文博士(0.5.2.1210)已经发布
    新一版的汉文博士(0.5.1.1070)已经发布
    EditPlus 3.5 版已经发布
    如何在计算机和汉字速查界面上显示七万个汉字
    使用汉字构形检索疑难字
    软件使用方法及界面截图
    Unihan 里的笔画数据有问题?
    循序渐进制作我的词典数据库(一)
  • 原文地址:https://www.cnblogs.com/hanjianfei/p/13214054.html
Copyright © 2020-2023  润新知