Import Urllib.request
#urllib. Request.urlopen can pass in a URL or request object
#req =urllib.request.request ("http://placekitten.com/g/500/600")
#response =urllib.request.urlopen (req)
#response的geturl, info (), GetCode () Get status, 200 indicates normal access
Response=urllib.request.urlopen ("http://placekitten.com/g/500/600")
Cat_img=response.read ()
With open (' cat_500_600.jpg ', ' WB ') as F:
F.write (CAT_IMG)
#get一般从服务器获得数据 can also be used to transmit data such as a single list.
#post传数据到服务器
Import Urllib.request
Import Urllib.parse
Import JSON
Content=input ("Please enter what you need to translate:")
Url= ' Http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule '
data={}
data[' I ']=content
data[' from ']= ' AUTO '
data[' to ']= ' AUTO '
data[' Smartresult ']= ' dict '
data[' client ']= ' Fanyideskweb '
data[' salt ']= ' 1520575049536 '
data[' sign ']= ' 4514C46C320493BA8C034EAA8D9DECAF '
data[' doctype ']= ' json '
data[' version ']= ' 2.1 '
data[' keyfrom ']= ' fanyi.web '
data[' action ']= ' fy_by_clickbuttion '
data[' Typoresult ']= ' false '
data[' UE ']= ' utf-8 '
#data需要特定的格式, you can use Urllib.parse.urlencode () to turn it into this form
#encode把unicode编码成utf-8 format
Data=urllib.parse.urlencode (data). Encode (' Utf-8 ')
#urlopen如果穿个data参数, then submit in the format of post, otherwise in get format
Response=urllib.request.urlopen (Url,data)
#decode是把unicode解码转成utf-8
Html=response.read (). Decode (' Utf-8 ')
Target=json.loads (HTML)
Print ("Translated Result:%s"%target[' Translateresult '][0][0][' TGT ')
Python crawler (2): Translation crawler