Python crawler (2): Translation crawler

Source: Internet
Author: User
Tags urlencode

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.