Use the requests module to crawl Baidu translation

Not much to say, just go to the code, if you don’t understand, you can refer to the last article I posted. This time I recommend you to use the Firefox browser, because I used the browser that comes with 2345 and win10, and I can’t get anything after running it successfully.

import requests
import json
if __name__ == '__main__':
    #1. 指定url
    post_url = 'https://fanyi.baidu.com/sug'
    #2. UA伪装
    headers = {
        'User Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36'
    }
    #3. 发送请求
    word = input('enter a word:')
    data = {
        'kw':word
    }
    response = requests.post(url=post_url,data=data,headers=headers)
    #4. 获取数据:json返回的是obj(如果确认响应数据是json类型的,才可以使用json())
    dic_obj = response.json()
    #5. 永久化存储
    fileName = word + '.json'
    a= open(fileName,'w',encoding='utf-8')
    json.dump(dic_obj,fp=a,ensure_ascii=False)#中文,所以json不能使用Ascii
    print('保存成功')

The results are as follows:

The file obtained after running is a json file, because the received data type is json type, which can be found on the web page

The contents of the open file are as follows, for example, the result of crawling love

Guess you like

Origin blog.csdn.net/qwerty1372431588/article/details/105900007