1. post请求
    # _*_ coding::utf_8 _*_
    import urllib.request
    import urllib.parse
    # 开始url、(word、data在一块)headers请求头
    post_url='https://fanyi.baidu.com/sug'
    word=input('输入查询单词:')
    form_data={
        'kw':word,
    }
    headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
    }
    #对data进行encode()操作,urlencode
    form_data=urllib.parse.urlencode(form_data).encode()
    # 伪装
    url=urllib.request.Request(url=post_url,headers=headers)
    #post请求中要加入data值
    response=urllib.request.urlopen(url,data=form_data)
    #输出post
    print(response.read().decode())
  2. get请求
    # _*_ coding:utf_8 _*_
    import urllib.request
    import urllib.parse
    content=input("输入要搜索的内容:")
    url='https://www.baidu.com/s?tn=93288632_hao_pg&ie=utf-8&sc=UWY4n1c3rjm1n-qCmyqxTAThIjYkPHm4nHRYPHcYP1bzFhnqpA7EnHc1Fh7W5HnLPH64rHRkPjT&ssl_sample=normal&srcqid=3499612321700897328&H123Tmp=nunew7&'
    data={
       'word':content
    }
    query_string=urllib.parse.urlencode(data)
    url=url+query_string
    response=urllib.request.urlopen(url)
    filename=content+'.html'
    with open(filename,'wb') as fp:
        fp.write(response.read())
01-13 19:23