Home  >  Article  >  Backend Development  >  Python crawls Baidu Translation (using json to extract data)

Python crawls Baidu Translation (using json to extract data)

不言
不言forward
2018-09-28 14:57:045365browse

The content of this article is about Python crawling Baidu translation (using json to extract data). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Tools: Python 3.6.5, PyCharm development tools, Windows 10 operating system

Description: This example is a small program that implements input Chinese translation into English, suitable for beginners of Python crawlers to learn together , those who are interested can use the function of translating English to Chinese, such as word query function, etc. It is recommended to use Google Chrome or Firefox to inspect elements. You need to install the module before using it: pip install request pip install json.

Data extraction method: json

1. Data exchange format, looks like a string of Python type (list, dictionary)

2. Need to import before using json

3.json.loads

 (1). Convert json string to Python type

 (2).json.loads(json string)

4. json.dumps

  (1) Convert Python type to json string

  (2) json.dumps({})

  (3 ), json.dumps(ret1, ensure_ascii=False,indent=2)

ensure_ascii allows Chinese to be displayed as Chinese

indent: allows the next line to be spaced based on the previous line

Code:

import requests
import json
url = "https://fanyi.baidu.com/basetrans"
query_str = input("请输入要翻译的中文:")
data = {
        "query":query_str,
        "from":"zh",
        "to":"en"}
headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",

        "Referer": "https://fanyi.baidu.com/?aldtype=16047&tpltype=sigma"
}
response = requests.post(url,data=data,headers=headers)
html_str = response.content.decode()#json字符串
#json数据交换格式,使用json之前需要导入
#把json字符串转化为Python类型
dict_ret = json.loads(html_str)
#print(dict_ret)
#print(type(dict_ret))
ret = dict_ret["trans"][0]["dst"]
print("翻译结果是:",ret)

Running effect:

The above is the detailed content of Python crawls Baidu Translation (using json to extract data). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete