浙江小D翻译读音

import requests

# 传入 音频url  和 单词名
def get_mpga(url,text):

    payload = ""
    headers = {
        'Accept-Encoding': "identity;q=1, *;q=0",
        'chrome-proxy': "frfr",
        'Range': "bytes=0-",
        'Referer': "https://dict.hjenglish.com/app/trans",
        'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
        'cache-control': "no-cache",
        'Postman-Token': "862d7519-6acb-446f-91f8-92ccd70150a9"
    }
    response = requests.request("GET", url, data=payload, headers=headers)

    r = response.content
    # print(response.text)
    fo = open('{}.mpga'.format(text), 'wb')  # 注意要用'wb',b表示二进制,不要用'w'
    fo.write(r)  # r.content -> requests中的二进制响应内容:以字节的方式访问请求响应体,对于非文本请求
    fo.close()
    print('写入成功 ')
    print('请查看当前文件夹下 .mpga后缀文件')

url = "https://dict.hjenglish.com/v10/dict/translation/cn/en"
print('请输入:')
text = input()

print('你刚才输入的是:{}'.format(text))
payload = {
    "content": "car",
}
payload["content"] = text
headers = {
    'authority': "dict.hjenglish.com",
    'method': "POST",
    'path': "/v10/dict/translation/cn/en",
    'scheme': "https",
    'accept': "*/*",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "zh-CN,zh;q=0.9",
    'content-length': "11",
    'content-type': "application/x-www-form-urlencoded; charset=UTF-8",
    'cookie': "acw_tc=707c9fdd15857108273406177e4724e68dbecc8a6e7bd6d9a3058aedcd043c; HJ_UID=66a7273b-3a19-b3bf-69f9-ee939b2f9b98; _SREF_3=; _REF=; _SREG_3=direct|; _REG=direct|; TRACKSITEMAP=3%2C; HJ_SID=a0e5074d-5ac6-93fc-8e2a-f1adfb2f5ec4; HJ_SSID_3=b40bf6f6-59af-9d4c-f399-4ebb266644b8; HJ_CST=0; HJ_CSST_3=0",
    'origin': "https://dict.hjenglish.com",
    'referer': "https://dict.hjenglish.com/app/trans",
    'user-agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    'x-requested-with': "XMLHttpRequest",
    'cache-control': "no-cache",
    'Postman-Token': "ccf8fd53-ecc6-4660-85fa-a8b5ba5f2c61"
    }

response = requests.request("POST", url, data=payload, headers=headers)
r = response.json()
mpga_url = r['data']['pronounce']['audio'][0]['url']
# print(r['data']['pronounce']['audio'][0]['url'])
get_mpga(mpga_url,text)

你可能感兴趣的:(requests,网页爬虫)