Tihool 发表于 2022-6-21 23:23:38

爬百度翻译

import requests
import json
defmain():
    url = 'https://fanyi.baidu.com/langdetect'
    header = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44',
      'Cookie':'__yjs_duid=1_ce56e5b7505c9ec2d1a24d8bb35798641619156507168; BIDUPSID=1368BB2DF719A8143DB57C26BBBF6890; PSTM=1619156543; BDUSS=FLNmxOZi13eWQ2Nmxsc0pBMGNEaUxrUUViT1l4MlBNMXhiUnFHcTl2b3RBZ2hoRVFBQUFBJCQAAAAAAAAAAAEAAABnopRnV1RpSG91AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC114GAtdeBgV1; BDUSS_BFESS=FLNmxOZi13eWQ2Nmxsc0pBMGNEaUxrUUViT1l4MlBNMXhiUnFHcTl2b3RBZ2hoRVFBQUFBJCQAAAAAAAAAAAEAAABnopRnV1RpSG91AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC114GAtdeBgV1; BAIDUID=86D6B7A1EBC52FE05E11B72EA9E70BF4:FG=1; APPGUIDE_10_0_2=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; REALTIME_TRANS_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; H_PS_PSSID=31253_26350; ZFY=foRFecIcTft5JJVDyuL57XidQkZOrSrv1xBE9ezbMrA:C; BAIDUID_BFESS=86D6B7A1EBC52FE05E11B72EA9E70BF4:FG=1;BDORZ=FFFB88E999055A3F8A630C64834BD6D0;Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1655818885,1655821353,1655822134;ab_sr=1.0.1_NGFhMzM5MDZmY2ExN2M5MmFkZDY3MDVhNGRkNGM4OGVmYjJlYWVlMzBkMGM3MDk5ZWYwNTU1YzgyM2FlMWMxY2Q3YmZmMGJiZDFiMTE5YWZlYWYxOWZjZjAzZDJhZGJjZTlmZWVkYjk2MzI3MTE1NmQyYzE3ODhlZTY2MDk1OWEzZWQ2NjM0M2ZmYmFlOTZkZWU5YmMxMTk2MTQ1Y2M3ZDZjMzVhYjVmZWU0ZGM1NjIzZjJiMTQ0YmJiNjNlMjYx;Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1655822154',
      'Referer': 'https: // fanyi.baidu.com /',
      'Origin':'https://fanyi.baidu.com',

    }



    datas = {
      'from': 'en',
      'query': 'holy',
      'to': 'zh',
      'domain': 'common',
      'simple_means_flag': '3',
      'token': 'c04a291efa684f3735abc2043d592377',
      'sign': '720154.925227'
    }
    response = requests.post(url = url,headers=header,data = datas)
    print(response.text)
if __name__ == "__main__":
    main()
为什么post请求不成功啊

suchocolate 发表于 2022-6-22 00:57:16

你这个请求是成功的,问题是你用错了url,百度返回的数据得访问另外的url。

suchocolate 发表于 2022-6-22 00:59:02

另外百度开放翻译api,注册一下就能调用正式的api,比这种爬取方便正规。
页: [1]
查看完整版本: 爬百度翻译