免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1944 | 回复: 9
打印 上一主题 下一主题

用perl在论坛发贴 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-25 07:05 |只看该作者 |倒序浏览
想用perl做一个论坛发贴的程序,对方论坛是discuz
程序如下

  1. #!c:\perl\bin\perl.exe

  2. use LWP;
  3. use HTTP::Cookies;

  4. $tid = 161531;
  5. $message = 'dddddd';
  6. $url = qq(http://www.dolc.de/forum/post.php?action=reply&fid=139&tid=$tid&replysubmit=yes);

  7. my $browser = LWP::UserAgent->;new;

  8. $browser->;cookie_jar({});
  9. $browser->;agent('Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1)');


  10. my $response = $browser->;post( $url,
  11.    [
  12.      'formhash' =>; '429acff1',
  13.      'message' =>; $message,
  14.    ],
  15. );

  16. if ($response->;is_success) {
  17.         print "success\n";
  18.         print $response->;content;
  19. } else {
  20.         print $response->;status_line;
  21. }
复制代码

程序返回结果说没有登陆
应该是没有cookie,应该怎么发送cookie
看了这篇文章http://www.perlchina.org/archive ... archive&page=15, 还是很不解, 是不是可以把机子上过去登录的cookie文件copy出来,然后指定这个文件的路径?
谢谢

论坛徽章:
0
2 [报告]
发表于 2005-04-25 11:26 |只看该作者

用perl在论坛发贴

内存式的 cookie 使用方法:

   $browser->;cookie_jar({});

调用上面这一行就会让当前这个 lwp 的 instance 自动记住 cookie. 你根本不用做其他的。

论坛徽章:
0
3 [报告]
发表于 2005-04-25 15:04 |只看该作者

用perl在论坛发贴

you say the instance will automatic remenber the cookie? But where come from this cookie? From the Browser cookie? How know it, which cookie is the right one?
Thank you

论坛徽章:
0
4 [报告]
发表于 2005-04-25 15:49 |只看该作者

用perl在论坛发贴

use HTTP::Cookies::Netscape;
my $cookie_jar = HTTP::Cookies::Netscape->;new(
'file' =>; 'c:/program files/netscape/users/me/cookies.txt'
);
$browser->;cookie_jar($cookie_jar);
可以读取netscape格式的cookie文件

论坛徽章:
0
5 [报告]
发表于 2005-04-25 16:59 |只看该作者

用perl在论坛发贴

netscape格式的cookie文件

Is it the same format as cookie of IE?

论坛徽章:
0
6 [报告]
发表于 2005-04-25 17:19 |只看该作者

用perl在论坛发贴

原帖由 "笨狗" 发表:

Is it the same format as cookie of IE?

This can be used for Cookie file produce by mozilla, firefox, netscape.
For IE i have on idea.

论坛徽章:
0
7 [报告]
发表于 2005-04-25 18:07 |只看该作者

用perl在论坛发贴

原帖由 "tiw" 发表:

This can be used for Cookie file produce by mozilla, firefox, netscape.
For IE i have on idea.

thanks a lot for your reply.
I will try later with firefox.

论坛徽章:
0
8 [报告]
发表于 2005-04-26 00:40 |只看该作者

用perl在论坛发贴

成功了,把代码贴出来

  1. #!c:\perl\bin\perl.exe

  2. use LWP;
  3. use HTTP::Cookies;
  4. use HTTP::Cookies::Netscape;

  5. $tid = 161531;
  6. $message = 'dddddd';
  7. $url = qq(http://www.dolc.de/forum/post.php?action=reply&fid=139&tid=$tid&replysubmit=yes);

  8. my $browser = LWP::UserAgent->;new;

  9. my $cookie_jar = HTTP::Cookies::Netscape->;new(
  10. 'file' =>; 'cookies.txt'
  11. );

  12. $browser->;cookie_jar($cookie_jar);
  13. $browser->;agent('Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1)');



  14. my $response = $browser->;post( $url,
  15.    [
  16.      'formhash' =>; '429acff1',
  17.      'message' =>; $message,
  18.    ],
  19. );

  20. if ($response->;is_success) {
  21.         print "success\n";
  22.         print $response->;content;
  23. } else {
  24.         print $response->;status_line;
  25. }
复制代码

cookies.txt是从firefox里面copy出来的,先删掉所有firefox的cookie,然后用firefox登录,把cookies.txt复制出来.
谢谢大家的帮忙
下一步是想个好点的办法定时run

论坛徽章:
0
9 [报告]
发表于 2005-04-26 01:49 |只看该作者

用perl在论坛发贴

sleep is valuable in Windows

论坛徽章:
0
10 [报告]
发表于 2005-04-26 07:55 |只看该作者

用perl在论坛发贴

你的程序应该先做登录,这样 $br 就会自动保存拿到的 cookie 。 有了 $browser->;cookie_jar({});  它在背后帮你做所有 HTTP::Cookie 的东西。使用 HTTP::Cookie 是老的方法。

假设我们有 $br instance. $br->;cookie_jar({});

1。 使用 $br->;post 模拟论坛登录。
2。 登录后,这时 $br 里就保存了 cookie
3。 现在你可以在论坛发贴
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP