1、引入urllib庫。
2、發(fā)起請(qǐng)求。
3、讀取返回的內(nèi)容。
4、編碼設(shè)置。(b'為二進(jìn)制編碼,需要轉(zhuǎn)化為utf-8)
5、打印出來。
import urllib.requestresponse=urllib.request.urlopen("http://www.baidu.com")html=response.read()html=html.decode("utf-8")PRint(html)二、下載圖片并保存到本地
import urllib.request#****this is the first way***#response = urllib.request.urlopen("https://img6.bdstatic.com/img/image/smallpic/weiju112.jpg")#****this is the second way***req = urllib.request.Request("https://img6.bdstatic.com/img/image/smallpic/weiju112.jpg")response=urllib.request.urlopen(req)cat_img = response.read()with open('aaaabbbbcccc.jpg','wb') as f: f.write(cat_img)3、有道翻譯import urllib.requestimport urllib.parseimport jsoncontent=input("Please input the content that you will translate:")url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link'data={}data['action']='FY_BY_CLICKBUTTON'data['doctype']='json'data['i']=contentdata['keyfrom']='fanyi.web'data['type']='auto'data['typoResult']='true'data['ue']='UTF-8'data['xmlVersion']='1.8'data=urllib.parse.urlencode(data).encode("utf-8") response=urllib.request.urlopen(url,data)html=response.read().decode('utf-8')res=json.loads(html) #res is a directprint("The result:%s" % (res['translateResult'][0][0]['tgt']))4、有道翻譯增加頭部信息(1)(通過增加header信息參數(shù),創(chuàng)建頭部字典)。import urllib.requestimport urllib.parseimport jsoncontent=input("Please input the content that you will translate:")url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link'head={} # the info of req.header to imitate the Agent just like visiting the website by browserhead['User-Agent']="Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"data={}data['action']='FY_BY_CLICKBUTTON'data['doctype']='json'data['i']=contentdata['keyfrom']='fanyi.web'data['type']='auto'data['typoResult']='true'data['ue']='UTF-8'data['xmlVersion']='1.8'data=urllib.parse.urlencode(data).encode("utf-8") #response=urllib.request.urlopen(url,data)req=urllib.request.Request(url,data,head)response=urllib.request.urlopen(req)html=response.read().decode('utf-8')res=json.loads(html) #res is a directprint("The result:%s" % (res['translateResult'][0][0]['tgt']))5、有道翻譯增加頭部信息(2)(通過Request.add_header())。
import urllib.requestimport urllib.parseimport jsoncontent=input("Please input the content that you will translate:")url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link''''head={} # the info of req.header to imitate the Agent just like visiting the website by browserhead['User-Agent']="Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"'''data={}data['action']='FY_BY_CLICKBUTTON'data['doctype']='json'data['i']=contentdata['keyfrom']='fanyi.web'data['type']='auto'data['typoResult']='true'data['ue']='UTF-8'data['xmlVersion']='1.8'data=urllib.parse.urlencode(data).encode("utf-8") #response=urllib.request.urlopen(url,data)req=urllib.request.Request(url,data)req.add_header('User-Agent',"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0")response=urllib.request.urlopen(req)html=response.read().decode('utf-8')res=json.loads(html) #res is a directprint("The result:%s" % (res['translateResult'][0][0]['tgt']))7、使用代理。
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注