import urllib.parse
import http.client as httplib
def get(url):
httpClient = None
params = urllib.parse.urlencode({"id":'-1767504348',"text":"查看家居产品,设备不断更新,便宜"})
print(params)
headers = {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'Content-Type':'application/json; charset=UTF-8',
'Cookie':'destDia=zh-CN; srcLang=zh-CHS; smru_list=zh-CHS; destLang=vi; dmru_list=da%2Czh-CHS%2Cvi; sourceDia=zh-CN; SRCHD=AF=WNSGPH; SRCHUSR=AUTOREDIR=0&GEOVAR=&DOB=20151223; MUIDB=04492A26332B65140ACA22A0372B6389; SRCHHPGUSR=CW=1903&CH=978&DPR=1; MUID=04492A26332B65140ACA22A0372B6389; SRCHUID=V=2&GUID=241945A2F1E146E1B95EA3D067A9A132; mtstkn=dno%2BRP9HUybhH26u2dFMzsxxqcmJsAj8%2Fm8DT2Z%2BdQQduROFh50TlTJ5tQJ2d7KS; _EDGE_S=SID=336F79E6160A6E1E088C70BF17AB6FCE; _SS=SID=336F79E6160A6E1E088C70BF17AB6FCE; WLS=TS=63604682338; SNRHOP=I=&TS=; destDia=zh-CN; destLang=vi; dmru_list=da%2Czh-CHS%2Cvi; srcLang=zh-CHS; smru_list=vi%2Czh-CHS; sourceDia=zh-CN',
'Host':'www.bing.com',
'Origin':'http://www.bing.com',
'Referer':'http://www.bing.com/translator',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'X-Requested-With':'XMLHttpRequest',
}
httpClient = httplib.HTTPConnection(url)
httpClient.request("POST","/translator/api/Translate/TranslateArray?from=zh-CHS&to=vi", params, headers)
response = httpClient.getresponse()
print (response.status)
print (response.reason)
print (response.read())
print (response.getheaders()) #获取头信息
url = "www.bing.com"
get(url)
输出结果:
text=%E6%9F%A5%E7%9C%8B%E5%AE%B6%E5%B1%85%E4%BA%A7%E5%93%81%EF%BC%8C%E8%AE%BE%E5%A4%87%E4%B8%8D%E6%96%AD%E6%9B%B4%E6%96%B0%EF%BC%8C%E4%BE%BF%E5%AE%9C&id=-1767504348
400
Bad Request
b'{\r\n "Message": "Invalid request",\r\n "Details": [\r\n "You must specify a JSON array of Translate requests in the POST body"\r\n ]\r\n}'
[('Cache-Control', 'no-cache'), ('Pragma', 'no-cache'), ('Content-Length', '134'), ('Content-Type', 'application/json; charset=utf-8'), ('Expires', '-1'), ('Server', 'Microsoft-IIS/8.5'), ('X-AspNet-Version', '4.0.30319'), ('X-Powered-By', 'ASP.NET'), ('Access-Control-Allow-Origin', '*'), ('X-MSEdge-Ref', 'Ref A: 0E3A2425C6114DD8AB77C45C829902B8 Ref B: EB5443741EE6590EE5AD2A959DE71CCF Ref C: Thu Jul 21 01:22:33 2016 PST'), ('Date', 'Thu, 21 Jul 2016 08:22:32 GMT')]