post requests get request

  1. post request
    Coding :: _ * _ # UTF_8 _ * _ 
    Import the urllib.request 
    Import The urllib.parse 
    # start url, (word, data on a) the request header headers 
    POST_URL = ' https://fanyi.baidu.com/sug ' 
    Word = iNPUT ( ' enter a query word: ' ) 
    form_data = {
         ' kW ' : Word, 
    } 
    headers = {
     ' User-Agent ' : ' the Mozilla / 5.0 (the Windows NT 10.0; Win64; x64-) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 75.0.3770.100 Safari / 537.36 ' 
    } 
    # of data for encode () operation, urlencode 
    form_data= Urllib.parse.urlencode (form_data) .encode () 
    # disguise 
    URL = urllib.request.Request (URL = POST_URL, headers = headers) 
    #post data request to add value 
    Response = the urllib.request.urlopen (URL, data = form_data) 
    # output POST 
    Print (response.read (). decode ())

     

  2. get request
    # _*_ 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())

     

Guess you like

Origin www.cnblogs.com/ybl20000418/p/11575265.html