百度翻译通过ajax动态生成页面,需要模拟post请求获取数据

打开抓包工具获取post请求包:

  • 请求url

  • 请求user-agent

  • 响应数据类型

  • 返回一个json字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- encoding: utf-8 -*-
'''
@文件 :spider.py
@Time :2020-12-29 16:27:21
@Author :Karcyril
@Software:Visual Studio Code
'''

import requests
import json

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400'
}


postUrl = 'https://fanyi.baidu.com/sug'

kw = input('enter a word')

params = {
'kw': kw
}

resp = requests.post(url=postUrl, data=params, headers=headers)

# 将json字符串转化为py字典对象
dicObj = resp.json()

# print(dicObj['data'][0]['v'])

# 将py字典储存为json文件
with open('./test.json', 'w+', encoding='utf-8') as f:
json.dump(dicObj, f, ensure_ascii=False)