learning python reptile (c) Baidu translation

#post request (carrying parameters)
# json response data is a set of data

First find the url by Ethereal
found Baidu translated using Ajax (partial refresh)
so look crawl XHR file
Here Insert Picture Description
XHR file returns a json file

Here Insert Picture Description
Ethereal, then fiddle with f12 or page using Google developer tools

# -*- coding: utf-8 -*-
import requests
import json
if __name__ == '__main__':
    #step1 指定url
    post_url ='https://fanyi.baidu.com/sug'
    #step2 UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
    }
    #step3 post请求参数出路(和get一样)
    word = input('enter a word:')
    data = {
        'kw':word
    }
    #step4 发送请求
    #url = posturl data= 参数字典
    response = requests.post(url=post_url,data=data,headers=headers)

    #step5 获取响应数据
    #json方法返回是obj(对象),如果确认响应数据是json类型的 才可以使用json()
    dic_obj = response.json()
    #test
    #print(dic_obj)

    #step6 保存返回的数据
    fileName =word+'.json'
    fp =open(fileName,'w',encoding='utf-8')
    #json中含有中文,所以要加上ensure_ascii=False
    json.dump(dic_obj,fp=fp,ensure_ascii=False)
    print(fileName,"完成")
Published 23 original articles · won praise 0 · Views 678

Guess you like

Origin blog.csdn.net/haimian_baba/article/details/103699601