鱼C论坛

 找回密码
 立即注册
查看: 96|回复: 2

一个叫HTTP Error 500: Internal Server Error的BUG

[复制链接]
发表于 4 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
刚刚开始学习Python,看了一个视频教程之后按照教程尝试,
但是结果出现了urllib.error.HTTPError: HTTP Error 500: Internal Server Error,
具体实现的功能是利用post请求百度翻译的详细翻译
代码如下:
  1. import urllib.request
  2. import urllib.parse
  3. import json
  4. url='https://fanyi.baidu.com/v2transapi?from=en&to=zh'

  5. headers={
  6.     'Accept':' */*',
  7.     # 'Accept-Encoding':' gzip, deflate, br',
  8.     'Accept-Language':' zh-CN,zh;q=0.9',
  9.     'Connection':' keep-alive',
  10.     'Content-Length':' 136',
  11.     'Content-Type':' application/x-www-form-urlencoded; charset=UTF-8',
  12.     'Cookie': 'BIDUPSID=E93B339658BDFE321960B93B70A9EAFD; PSTM=1674224026; BAIDUID=E93B339658BDFE322A893B54A7CDEDEA:FG=1; BAIDUID_BFESS=E93B339658BDFE322A893B54A7CDEDEA:FG=1; ZFY=MSuAnxENbgH:AEqQwSOP2FlC0:AruCTwf:BbacXBcV58ZM:C; APPGUIDE_10_0_2=1; FANYI_WORD_SWITCH=1; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; ariaDefaultTheme=undefined; RT="z=1&dm=baidu.com&si=e5hb03k1fsn&ss=ld8xlz6v&sl=6&tt=33f&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=gco&ul=vl8&hd=vm0"; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; SEARCH_MARKET_ABG_URL=http%3A//abg.baidu.com/ndoffhome/search%3Fquery%3D%25E7%259C%258B%25E4%25B9%25A6%26fileType%3D%26fr%3Dlaunch_ad%26SS-bdtg24%3D%26utm_source%3Dbdss-WD%26utm_medium%3Dcpc%26utm_account%3DSS-bdtg24%26utm_brand%3Dabg%26e_creative%3D68600065689%26e_keywordid%3D536869508575%26bd_vid%3D10644333436569472536; BDRCVFR[za_m8Iw8qNT]=mk3SLVN4HKm; H_PS_PSSID=; BA_HECTOR=al84a52h050h052la50485411ht174b1k; PSINO=1; delPer=0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1674529315,1674548995,1674549837,1674615967; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1674615967; ab_sr=1.0.1_MTcyOTg0NzA4YzA3NWJiYzFlYmUwM2E5ZGEwNmZkNjkwMjczNDVlNzgzMzNiZTc4ZDc1ODQyYzg4YTI5NDY2MzBkYjJjZTFlMDA1M2Q3Y2Y3M2FiYzM5OTkyYTgyZDdhZmE4OTA1MzNhZGE4ZWIyNTczNjQ5NmQ5ZjliMzRhNWNiZTZlNmMyZWFjZjE4ZjZiYjdiMTc0N2E2MjY4NDUxMg==',
  13.     'Host':' fanyi.baidu.com',
  14.     'Origin': 'https://fanyi.baidu.com',
  15.     'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
  16.     'sec-ch-ua':' "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
  17.     'sec-ch-ua-mobile':' ?0',
  18.     'sec-ch-ua-platform':' "Windows"',
  19.     'Sec-Fetch-Dest':' empty',
  20.     'Sec-Fetch-Mode':' cors',
  21.     'Sec-Fetch-Site':' same-origin',
  22.     'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
  23.     'X-Requested-With':' XMLHttpRequest',
  24. }

  25. data={
  26.     'from':' en',
  27.     'to':' zh',
  28.     'query':' spider',
  29.     'simple_means_flag':' 3',
  30.     'sign':' 63766.268839',
  31.     'token':' 53b42a52057b8a1818a9497203c34625',
  32.     'domain':' common',
  33. }

  34. data=urllib.parse.urlencode(data).encode('utf-8')

  35. request=urllib.request.Request(url,data,headers)

  36. response=urllib.request.urlopen(request)

  37. content=response.read().decode('utf-8')

  38. print(content)
  39. obj=json.loads(content)
  40. print(obj)
复制代码


报出的错误是:
Traceback (most recent call last):
  File "D:\桌面文件夹\Python基础\pythonDemo3.py", line 234, in <module>
    response=urllib.request.urlopen(request)
  File "D:\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "D:\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "D:\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "D:\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "D:\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Internal Server Error
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 4 天前 | 显示全部楼层
baidu网站早更新了。过期了。知道原理就行

论坛 搜 百度翻译 应该有最新的代码范例
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 3 天前 | 显示全部楼层
ba21 发表于 2023-1-25 21:18
baidu网站早更新了。过期了。知道原理就行

论坛 搜 百度翻译 应该有最新的代码范例

感谢提醒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1

GMT+8, 2023-1-29 17:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表