03.Ajax-百度翻译

1.Ajax: 异步的js+xml,利用js在保证页面不被刷新、页面链接不改变的情况下与服务器交换数    据并更新部分网页的技术。

2.解析方法:

    打开网页开发者工具/网络/type中找到xhr

    请求头信息中:X-Requested-With:XMLHttpRequest,表示此请求为Ajax请求。

import requests
import json
url="https://fanyi.baidu.com/sug"
headers={
    "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0"
}

kw=input("请输入您要查询的单词:")
data={
    'kw':kw
}
reponse=requests.post(url=url,headers=headers,data=data)
page_json=reponse.json()
print(page_json)

fileName=kw+'.json'
fp=open(fileName,'w',encoding='utf-8')
json.dump(page_json,fp=fp,ensure_ascii=False)

 

    

   

posted @ 2020-12-08 11:33  nan4457  阅读(85)  评论(0)    收藏  举报