current position:Home>Baidu translation in Python

Baidu translation in Python

2022-07-24 16:31:30Call me Ajie

I hope it can be helpful to friends who are beginning to learn reptiles .

List of articles



One 、 Analyze the web

 1. Open the web page , Enter Baidu translation in the search box and enter Baidu translation website .F12 Call up developer tools , Click on Network( The Internet )\ Fetch/XHR, At the same time, enter any search content in the translation box , At this point, you will find a name called sug My bag . Click the package , Click to see Heders、Payload、Preview and Rsponse Options such as . Click on Heders Options , take Request URL: The website at the back Copy , this Is what we need in our code url.

  2. stay Heders Select the mouse wheel to the bottom , find User-Agent: And put the following parameters Copy , Namely What we need headers( Request header parameters ).

  3. Click in the page Payload Options , stay Form Data Under the option, we Meeting notice Yes Parameters of key value pairs . Ahead ” key “ Equivalent to the search box in Baidu translation , The latter value is the translation content we entered . These parameters are data Parameters ,data Parameters are passed in a dictionary , So this ” key “ namely kw That's what we need data Key for parameter .

  An exciting moment , Find the above parameters and you can go to the next step !!!

Two 、 Use steps

1. Import library

The code is as follows ( Example ):

import requests     
import json

2. Keyboard input

The code is as follows ( Example ):

fan_yi = input(" Please input the content to be translated :")    # 2. Keyboard input translation 

It's used here url Data requested by the network .


3. structure url、headers、data Parameters

    Inside headers Parameters may report errors due to different devices , You can find the parameters of your computer browser according to the web page analysis above , Just copy it .

  matters needing attention :headers Parameters exist in the form of dictionaries , Its keys and values are in string format , also User-Agent In the parameter U Letters and A All letters must be capitalized , If you write wrong carelessly, you will report an error .

The code is as follows ( Example ):

import requests     # 1. Import library 
import json
fan_yi = input(" Please input the content to be translated :")    # 2. Keyboard input translation 
url = "https://fanyi.baidu.com/sug"     # 3. write in url
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) ""AppleWebKit/537.36 "}  # 4. structure headers
data = {"kw":fan_yi}    # 5. structure data
rsponse1 = requests.post(url = url,headers = headers,data = data)   # 6. Initiate request 
rsponse2 = rsponse1.text    #  Get response data 
jie_xi = json.loads(rsponse2)   #  Parsing data 
print(jie_xi)   #  Output results 

4. Initiate request , The response data

The code is as follows ( Example ):

rsponse1 = requests.post(url = url,headers = headers,data = data)   #  Initiate request 
rsponse2 = rsponse1.text    #  Get response data 

5. Parsing data , Output results

The code is as follows ( Example ):

jie_xi = json.loads(rsponse2)   #  Parsing data 
print(jie_xi)   #  Output results 

3、 ... and 、 Complete code

  matters needing attention :headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) ""AppleWebKit/537.36 "} In this part "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ""AppleWebKit/537.36 " I have deleted it, so copying it directly will report an error , Because I found my computer browser according to the following figure User-Agent: The following parameters are copied into the code modify Talent .

   Say important things three times !!!:headers Parameters exist in the form of dictionaries , Its keys and values are in string format , also User-Agent In the parameter U Letters and A All letters must be capitalized , If you write wrong carelessly, you will report an error .

   Say important things three times !!!:headers Parameters exist in the form of dictionaries , Its keys and values are in string format , also User-Agent In the parameter U Letters and A All letters must be capitalized , If you write wrong carelessly, you will report an error .

   Say important things three times !!!:headers Parameters exist in the form of dictionaries , Its keys and values are in string format , also User-Agent In the parameter U Letters and A All letters must be capitalized , If you write wrong carelessly, you will report an error .

Method 1 : Write in a proper manner

The code is as follows ( Example ):

import requests     #  Import library 
import json
fan_yi = input(" Please input the content to be translated :")    #  Keyboard input translation 
url = "https://fanyi.baidu.com/sug"     #  write in url
#  structure headers
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) ""AppleWebKit/537.36 "}  
data = {"kw":fan_yi}     structure data
rsponse1 = requests.post(url = url,headers = headers,data = data)   #  Initiate request 
rsponse2 = rsponse1.text    #  Get response data 
jie_xi = json.loads(rsponse2)   #  Parsing data 
print(jie_xi)   #  Output results 

Method 2 : Encapsulate code into functions

def fangYi(data1):
    url = "https://fanyi.baidu.com/sug"
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)
    data = {"kw":data1}
    rsponse1 = requests.post(url = url,headers = headers,data = data)
    rsponse2 = rsponse1.content.decode()
    json1 = json.loads(rsponse2)
    print(json1)

if __name__ == '__main__':
    while True:
        data1 = input("+++++ Please enter the content to be translated below , Exit please enter ”no“+++++\n\t Please input the content to be translated :")
        if data1 == "no":
            break
        else:
            fangYi(data1)

copyright notice
author[Call me Ajie],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/205/202207241627583923.html

Random recommended