how could I set cookie followed by a redirect request? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shanechiu
V2EX    Python

how could I set cookie followed by a redirect request?

  •  
  •   shanechiu
    shanezhiu 2017-12-08 16:27:08 +08:00 2487 次点击
    这是一个创建于 2943 天前的主题,其中的信息可能已经有所发展或是发生改变。

    There was problem when I tried to grab BAIDU tongji infor.

    When I logined success, there was a 302 redirection to main page.

    That means the internal redirect was from https://tongji.baidu.com/web/welcome/ico?s=sdfsdfsdfsdf to s://tongji.baidu.com/web/12323243/overview/index?siteId=sdfsf.

    I wonder know that how does the program(may be the broswer? I am also not clear. LOL) pass the cookie from the 320 page to the destination page? and Why?

    Could anyone do me a favor? Thanks in advace.

    Append:

    302 page : https://tongji.baidu.com/web/welcome/ico?s=sdfsdfsdfsdf

    destination page: s://tongji.baidu.com/web/12323243/overview/index?siteId=sdfsf

    12 条回复    2017-12-09 17:43:19 +08:00
    Cooky
        1
    Cooky  
       2017-12-08 16:31:01 +08:00 via Android
    Chinese please or go to Stack Overflow
    shanechiu
        2
    shanechiu  
    OP
       2017-12-08 16:34:56 +08:00
    @Cooky I am a little worried about whether this question lives up to the Stack Overflow's strict standard.
        3
    vincenttone  
       2017-12-08 16:35:05 +08:00
    不知道你看得懂中文不
    中文答案:
    1. http 是无状态的
    2. cookie 是通过 header 传递的
    3. 留意一下 cookie 的域
    hhacker
        4
    hhacker  
       2017-12-08 16:38:11 +08:00
    Because the cookie is shared by same domain
    shanechiu
        5
    shanechiu  
    OP
       2017-12-08 16:43:35 +08:00
    @vincenttone well, Is it that means the 302 page request cookie will also pass to the destination page by header and it also acts as a request cookie in the destination page?
    fml87
        6
    fml87  
       2017-12-08 16:50:47 +08:00
    logined 是什么
    shanechiu
        7
    shanechiu  
    OP
       2017-12-08 16:54:05 +08:00
    @fml87 a past tense of word "login", it means events or actions happen in the past.
    vincenttone
        8
    vincenttone  
       2017-12-08 16:54:42 +08:00
    @shanechiu 如果你想理解 cookie 在 302 页面中的表现,就必须先了解 cookie 在普通页面中的表现。
    如我刚才所说:
    1. http 是无状态的
    这个是前提。
    cookie 存在本地,无状态的情况下,不关心你有没有做 302 跳转。
    shanechiu
        9
    shanechiu  
    OP
       2017-12-08 17:03:15 +08:00
    @vincenttone Thanks for your kindness and patience. There seems like a outline about this.
    knightdf
        10
    knightdf  
       2017-12-08 18:04:17 +08:00
    这么秀的吗?看历史原来你不是会中文么?
    yospan
        11
    yospan  
       2017-12-09 15:19:00 +08:00
    之前刚做了,用 session 啊,统计后台设置个第三方密码,然后 post 给他,保持 session 去请求其他页面,接着统计里的数据随便拿~ 那去参考下把;我是 py 新手;

    ```
    ##百度统计的第三查看密码,登录并获得 session 和 siteid
    idwd = {'passwd': '66666'}
    S = Session()
    logined = S.post("https://tongji.baidu.com/web/welcome/ico?s=8dfdafdafadfa4bccd", data=idwd, headers=REQ_HEADERS)

    #获得 siteid,并转换成字符串
    siteid= str(logined.url.split("=")[1])
    webid = str(logined.url.split("/")[4])



    ##搜索词的 post 参数
    keyjson = {"siteId":siteid,"st":"","et":"","st2":"","et2":"","indicators":"['pv_count','visitor_count','ip_count','bounce_ratio','avg_visit_time']","order":"pv_count,desc","offset":"","pageSize":"","target":"-1","flag":"indicator","source":"","isGroup":"0","clientDevice":"all","reportId":"12","method":"source/searchword/a","queryId":""}

    readkeyjson = S.post("https://tongji.baidu.com/web/"+webid+"/ajax/post", data=keyjson, headers=REQ_HEADERS)


    #按文本读取
    jsOndata= readkeyjson.text
    #格式化 json
    readjsOndict= json.loads(jsondata)

    keyNamejson = readjsondict['data']['items'][0]
    for items in keyNamejson:
    items2 = items
    print(items2[0]['name'])

    ```
    shanechiu
        12
    shanechiu  
    OP
       2017-12-09 17:43:19 +08:00
    @yospan well, I do not know whether your method is workable. But thank you all the same.
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3361 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 10:46 PVG 18:46 LAX 02:46 JFK 05:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86