Python论坛  - 讨论区

标题:[python-chinese] 问一个关于Python程序访问需要cookie的网页的问题

2007年05月08日 星期二 15:45

cao max_cao cmaxcao在gmail.com
星期二 五月 8 15:45:15 HKT 2007

hi,

    ÎÒ´ÓÍøÉÏÕÒÁ˸öÀý×Ó£¬ ÊÔ×ÅдÁ˸ö£¬ µ«ÊǺÃÏñ×ܲ»Äܵõ½ÐèÒªµÄÒ³Ã棬 ÿ´Î¶¼ÊÇתµ½loginÒ³Ãæ
    ³ÌÐòÈçÏ£¬

def main():

    url = "http://www.theknot.com/join/me_loginmain.aspx"
    cookie = cookielib.CookieJar()
    cp = urllib2.HTTPCookieProcessor(cookie)

    opener = urllib2.build_opener(cp)
    urllib2.install_opener(opener)

    line = urllib.urlencode({'tdEmail' : *** ,'tbPassword' : ***})


    url =
http://www.theknot.com/kl_vendordetail.html?id=115349&vendorsearch;=Y&market;=091&category;=CAR&keyword;=NEW%20YORK
    response = urllib2.urlopen(url, line )
    lines = response.read()

    f = open( "pagecode.txt", "w" )
    for line in lines:
        f.write( line )
    f.close()

    response.close()

main()


´ó¼ÒÖªµÀÔ­Òò²»£¿

thanks
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070508/297d8057/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月08日 星期二 17:48

Hong Yuan hongyuan在homemaster.cn
星期二 五月 8 17:48:45 HKT 2007

ºÃÏñû¼ûÄã´ò¿ªloginÒ³Ãæѽ£¿µÚÒ»¸öurlÓÃÔÚÄǶùÁËÄØ£¿

ÓÃpython·ÃÎÊ´øcookie¡¢ÐèÒªÌîformµÈµÄÍøÒ³µÄ»°£¬ÓÃmechanize¿âÄ£Äâä¯ÀÀÆ÷»á
±È½Ï·½±ã£¬ËüÄܹ»×Ô¶¯´¦Àícookie£¬Äܹ»·½±ãµÄÌîform¡£

cao max_cao дµÀ:
> hi,
>
> ÎÒ´ÓÍøÉÏÕÒÁ˸öÀý×Ó£¬ ÊÔ×ÅдÁ˸ö£¬ µ«ÊǺÃÏñ×ܲ»Äܵõ½ÐèÒªµÄÒ³Ã棬 ÿ´Î
> ¶¼ÊÇתµ½loginÒ³Ãæ
> ³ÌÐòÈçÏ£¬
>
> def main():
>
> url = "http://www.theknot.com/join/me_loginmain.aspx"
> cookie = cookielib.CookieJar()
> cp = urllib2.HTTPCookieProcessor(cookie)
>
> opener = urllib2.build_opener(cp)
> urllib2.install_opener(opener)
>
> line = urllib.urlencode({'tdEmail' : *** ,'tbPassword' : ***})
>
>
> url =
> http://www.theknot.com/kl_vendordetail.html?id=115349&vendorsearch;=Y&market;=091&category;=CAR&keyword;=NEW%20YORK
> <http://www.theknot.com/kl_vendordetail.html?id=115349&vendorsearch;=Y&market;=091&category;=CAR&keyword;=NEW%20YORK>
> response = urllib2.urlopen(url, line )
> lines = response.read()
>
> f = open( "pagecode.txt", "w" )
> for line in lines:
> f.write( line )
> f.close()
>
> response.close()
>
> main()
>
>
> ´ó¼ÒÖªµÀÔ­Òò²»£¿
>
> thanks
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


-- 

´ó¹Ü¼ÒÍøÉϽ¨²Ä³¬ÊÐ
×°ÐÞÂò½¨²Ä ÉÏÍøÕÒ´ó¹Ü¼Ò
http://www.homemaster.cn
Tel: 0086-21-34240987
Fax: 0086-21-64692422

-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070508/5cd73b5d/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月08日 星期二 18:07

Bruce Wang number5在gmail.com
星期二 五月 8 18:07:37 HKT 2007

用twill

On 5/8/07, Hong Yuan <hongyuan在homemaster.cn> wrote:
>
>  好像没见你打开login页面呀?第一个url用在那儿了呢?
>
> 用python访问带cookie、需要填form等的网页的话,用mechanize库模拟浏览器会比较方便,它能够自动处理cookie,能够方便的
> 填form。
>
> cao max_cao 写道:
>
> hi,
>
>     我从网上找了个例子, 试着写了个, 但是好像总不能得到需要的页面, 每次都是转到login页面
>     程序如下,
>
> def main():
>
>     url = "http://www.theknot.com/join/me_loginmain.aspx">
>     cookie = cookielib.CookieJar()
>     cp = urllib2.HTTPCookieProcessor(cookie)
>
>     opener = urllib2.build_opener(cp)
>     urllib2.install_opener(opener)
>
>     line = urllib.urlencode({'tdEmail' : *** ,'tbPassword' : ***})
>
>
>     url = http://www.theknot.com/kl_vendordetail.html?id=115349&vendorsearch;=Y&market;=091&category;=CAR&keyword;=NEW%20YORK
>
>     response = urllib2.urlopen(url, line )
>     lines = response.read()
>
>     f = open( "pagecode.txt", "w" )
>     for line in lines:
>         f.write( line )
>     f.close()
>
>     response.close()
>
> main()
>
>
> 大家知道原因不?
>
> thanks
>
> ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> --
>
> 大管家网上建材超市
> 装修买建材 上网找大管家
> http://www.homemaster.cn
> Tel: 0086-21-34240987
> Fax: 0086-21-64692422
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
simple is good
http://brucewang.net
skype: number5
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070508/a31f218f/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月08日 星期二 18:46

cao max_cao cmaxcao在gmail.com
星期二 五月 8 18:46:19 HKT 2007

Hong Yuan £º²»ºÃÒâ˼£¬ ûÓÐдÇå³þ

    url = "http://www.theknot.com/join/me_loginmain.aspx">
    cookie = cookielib.CookieJar()
    cp = urllib2.HTTPCookieProcessor(cookie)

    opener = urllib2.build_opener(cp)
    urllib2.install_opener(opener)

    str = urllib.urlencode({'tdEmail' : *** ,'tbPassword' : ***})
    page = urllib2.urlopen(url, str) #ÉÏÃæ¿ÉÒÔ½«cookie±£´æÏÂÀ´£¬

    #ÏÂÃæµÄ²¿·ÖÓ¦¸Ã¾Í¿ÉÒÔÖ±½Ó·ÃÎÊËùÓеÄÒ³ÃæÁË
    url = http://www.theknot.com/kl_vendordetail.html?id=115349&vendorsearch;=Y&market;=091&category;=CAR&keyword;=NEW%20YORK

    response = urllib2.urlopen(url, str )
    lines = response.read()

    f = open( "pagecode.txt", "w" )
    for line in lines:
        f.write( line )
    f.close()

    response.close()

ÎÒ¾õµÃºÃÏñcookieûÓб£´æÏÂÀ´£¬ËùÒÔûÓа취ȡµÃÏëÒªµÄÒ³Ãæ
 ллÄã
ÔÚ07-5-8£¬Hong Yuan <hongyuan在homemaster.cn> дµÀ£º
>
>  ºÃÏñû¼ûÄã´ò¿ªloginÒ³Ãæѽ£¿µÚÒ»¸öurlÓÃÔÚÄǶùÁËÄØ£¿
>
> ÓÃpython·ÃÎÊ´øcookie¡¢ÐèÒªÌîformµÈµÄÍøÒ³µÄ»°£¬ÓÃmechanize¿âÄ£Äâä¯ÀÀÆ÷»á±È½Ï·½±ã£¬ËüÄܹ»×Ô¶¯´¦Àícookie£¬Äܹ»·½±ãµÄ
> Ìîform¡£
>
>
> <http://python.cn/mailman/listinfo/python-chinese>
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070508/1359e333/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号