import urllib.request
import urllib.parse

def get(urlStr,params={}):
    if params == {}:
        req = urllib.request.urlopen(urlStr)
    else:
        reqdata = urllib.request.Request(urlStr,urllib.parse.urlencode(params).encode('ascii'))
    req = urllib.request.urlopen(reqdata)
    return req.read()

import urllib.request
import urllib.parse

def get(urlStr,params={}):
    reqdata = urllib.request.Request(urlStr)
    reqdata.add_header('User-Agent',
                       'VocabTool/0.2 (https://github.com/RihanWu/vocabtool)')
    if params != {}:
        reqdata.data = urllib.parse.urlencode(params).encode('ascii')
    req = urllib.request.urlopen(reqdata)
    return req.read()

Add User-Agent for better netiquette
Add User-Agent for better netiquette

  import urllib.request
  import urllib.parse
  
  def get(urlStr,params={}):
+     reqdata = urllib.request.Request(urlStr)
+     reqdata.add_header('User-Agent',
+                        'VocabTool/0.2 (https://github.com/RihanWu/vocabtool)')
-     if params == {}:
+     if params != {}:
-         req = urllib.request.urlopen(urlStr)
-     else:
-         reqdata = urllib.request.Request(urlStr,urllib.parse.urlencode(params).encode('ascii'))
+         reqdata.data = urllib.parse.urlencode(params).encode('ascii')
      req = urllib.request.urlopen(reqdata)
      return req.read()
  
Add User-Agent for better netiquette
## Code Before:
import urllib.request
import urllib.parse

def get(urlStr,params={}):
    if params == {}:
        req = urllib.request.urlopen(urlStr)
    else:
        reqdata = urllib.request.Request(urlStr,urllib.parse.urlencode(params).encode('ascii'))
    req = urllib.request.urlopen(reqdata)
    return req.read()

## Instruction:
Add User-Agent for better netiquette
## Code After:
import urllib.request
import urllib.parse

def get(urlStr,params={}):
    reqdata = urllib.request.Request(urlStr)
    reqdata.add_header('User-Agent',
                       'VocabTool/0.2 (https://github.com/RihanWu/vocabtool)')
    if params != {}:
        reqdata.data = urllib.parse.urlencode(params).encode('ascii')
    req = urllib.request.urlopen(reqdata)
    return req.read()
