Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python as a translator

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use python to do translation program". In daily operation, I believe many people have doubts about how to use python to do translation program. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use python to do translation program". Next, please follow the editor to study!

First, open the URL translated by youdao:

"http://fanyi.youdao.com/"

Press f12 to open the inspection tool

Click element to browse.

Found that there is no content in html that we want.

It means that it is not statically loaded.

It should be loaded by dynamic Ajax.

So click Network and start looking like this.

You'll see there's nothing.

Press F5 to refresh and many URLs or files will appear.

These documents are part of Youdao Translation's web page.

Load from top to bottom

But don't refresh here.

Otherwise, it will take time to find the corresponding information later.

Write something casually in place 1

Then click XHR (it doesn't matter if you click first or later)

Then a URL will pop up in three places.

Click headers and you will see the content in the fourth place.

Click preview

You will find that there are contents and results that we want to translate.

Looks like we're going in the right direction.

It stores data in json format

It will be more convenient for us to extract data.

Unexpectedly, after knowing the URL, it is to extract the data.

So how do we extract it?

Back to our headers, there is a web site in the fourth place.

This URL is the one that really stores the data we need.

Keep looking down.

Find Request Method.

It turns out that it makes requests to the server in post mode.

So what data are we going to submit?

Keep looking down.

Found what we want to translate in From data.

It seems that the following paragraph is the data we need to submit.

How do we get so much data?

Don't worry.

We try to translate something else.

See which values will change.

After comparison, it is found that

Four values have changed.

They are the four "iMagnesia saltsignsignsignmt ts".

I is what we want to translate.

I don't know what the other three are.

Later, I found that these three can be done without care.

After knowing where our target is,

Is to write code to extract data.

Because it's in json format.

Equivalent to a dictionary

So it's easy to get.

Because there's not much code.

It won't be hard to understand.

So there are some aspects that I won't elaborate on.

Go directly to the code

Import requestsurl = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"data = {" I ": input (" enter what to be translated: ")," from ":" AUTO "," to ":" AUTO "," smartresult ":" dict "," client ":" fanyideskweb "," bv ":" 146ae0587a435b36d5e7ac4d9b08bde6 "," doctype ":" json "," version ":" 2.1", "keyfrom": "fanyi.web" "action": "FY_BY_REALTlME"} req = requests.post (url,data = data) # in post form js = req.json () result = js ["translateResult"] [0] [0] ["tgt"] print (result)

The requests object has a request to the server in two ways

One is the get request.

One is today's post request.

What's the difference between the two?

There is no need to submit a form for an get request

That is, there is no need to submit additional data.

It is generally used to download static web pages and so on.

The post request requires us to provide some data to the server.

For example, to log in is to use post request.

Then there is a data parameter in the post method

Used to store submitted data

It is stored in the data structure of a dictionary

Because we need to extract data from json format.

So we use the json method to convert the downloaded data into json format.

In the end, just like extracting a dictionary, you will OK.

At this point, the study on "how to use python as a translation program" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report