How to Build a Python Scraper for Youdao Mobile Translation API
This tutorial walks you through using Python's requests and lxml libraries to reverse‑engineer the Youdao mobile translation interface, construct the required form parameters, send POST requests, parse the returned HTML with XPath, and display translated results for multiple languages.
Project Background
Youdao Translate is one of the largest translation apps in China with a huge user base; users often turn to it for quick word or sentence translations.
This article shows how to obtain the mobile translation API used by Youdao.
Project Goal
Enable translation of words or sentences across multiple languages by calling the API.
Libraries and Websites
URL: http://m.youdao.com/translate
Libraries: requests , lxml
Software: PyCharm
Project Analysis
1. Open developer tools (F12), switch to mobile mode, go to the Network panel, and locate the form data under the request headers.
2. The request contains two parameters: inputtext (the word or sentence) and type (the language code).
3. Build a form that sends these two parameters to achieve multi‑language translation.
4. Parse the returned page using XPath to extract translation results.
Implementation
1. Import the required libraries and define a YoudaoSpider class with an initializer.
import requests
from lxml import etree
class YoudaoSpider(object):
def __init__(self):
pass
if __name__ == '__main__':
spider = YoudaoSpider()Prepare URL and headers
self.trans_url = "http://m.youdao.com/translate"
self.headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}Define get_result method
def get_result(self, word, fro):
"""Construct form data dictionary"""
data = {
'inputtext': word,
'type': fro
}
response = requests.post(url=self.trans_url, data=data, headers=self.headers)
html = response.content.decode('utf-8')
pa = etree.HTML(html)
pa_list = pa.xpath("//div[@class='generate']/ul/li/text()")
for i in pa_list:
print("翻译为:" + i)5. Choose the language type and call get_result:
choice = input("1.中翻译英语 2.中翻韩语 3.把中文翻译法语
请选择1/2/3:
")
if choice == '1':
fro = 'ZH_CN2EN'
elif choice == '2':
fro = 'ZH_CN2SP'
elif choice == '3':
fro = 'ZH_CN2FR'
word = input("请输入你要翻译的单词或句子:")
result = spider.get_result(word, fro)Result Demonstration
1. Input the translation type.
2. Input the sentence to translate.
Summary
The solution uses a Python web scraper to access Youdao's translation API.
It explains how to construct the request form and parse the response with XPath.
Readers are encouraged to experiment with other languages to deepen their understanding of crawling principles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
