fch024205
2021-09-15 17:09
采纳率: 100%
浏览 31
已结题

python爬虫request.get响应为505,如何解决?

import requests
import time
# cookie 为从浏览器复制过来的cookie值
cookie = 'kkb_edu_session=eyJpdiI6Im52a2VnWjVId1owVkFHcFA3TVJKWGc9PSIsInZhbHVlIjoiZ0h4ODlEbERIQjd4cnF6eFZPSUN5bW41MitCbGNISFVseDNaZVFicG9qUER4WUVFSlUwVitqOWd4Q1lYMUhDMCIsIm1hYyI6ImQzNjI3MjJmNmRkNmI4MWM3YWZhNTBlYmYxZWM2M2ZiYWM2ZjJlYjBkMzc2NTEzNmNjZDhlZDQ1MTliOTQxZjUifQ%3D%3D; expires=Thu, 16-Sep-2021 06:53:56 GMT; Max-Age=86400; path=/; domain=kaikeba.com;'
cookies = {i.split("=")[0]:i.split("=")[1] for i in cookie.split("; ")}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
    "origin": "https://learn.miduiedu.com",
    "referer": "https://learn.miduiedu.com/"
}
#
url = 'http://weblearn.miduiedu.com/student/courseinfo?course_id=216992&__timestamp={}'.format(int(time.time()*1000))
print(url)
response = requests.get(url = url ,headers=headers,cookies=cookies)
print(response.text)

​

问题描述:一个在线学习网站,登录后将cookie保存,然后用request.get 获得的状态码是:200

但是获取的text内容为:

{"code":-505,"data":{"app_id":"hky8475638475664"},"msg":"\u8bf7\u5148\u767b\u5f55"}

经过json格式解码为:

{
    "code":-505,
    "data":{
        "app_id":"hky8475638475664"
    },
    "msg":"请先登录"
}

有没有遇见过相似问题的同学?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-09-15 17:32
    已采纳

    你缺少了一个很重要的请求头authorization,cookie什么其他的经测试可以不传递也能正确获取到信息,有帮助麻烦点个采纳【本回答右上角】,谢谢~~

    img

    img

    
    import requests
    import time
    
    headers = {
        'authorization':'Bearer pc:xxxxxx'#这里xxxxxx的值看浏览器发送的请求头部分,如上图所示
    }
    #
    url = 'https://weblearn.miduiedu.com/student/courseinfo?course_id=222444&__timestamp={}'.format(int(time.time()*1000))
    print(url)
    
    response = requests.get(url = url ,headers=headers)
    print(response.text)
    
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题