2

I am trying to update status on facebook using Mechanize.I am able to login using the script but unable to update.I verified the id of form for status update is "u_0_w". But selecting the form_id method says "There is no form with ID "u_0_w"". My script is this:

use WWW::Mechanize;
use strict;
use warnings;
use Data::Dumper;
use HTTP::Cookies::Netscape;

my $cookiesfilename='/home/xxx/xxx/cookies.txt';
my $out;
my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies::Netscape->new( file => $cookiesfilename ) );
$mech->get("https://www.facebook.com/login.php");
my $response=$mech->submit_form(
    fields => {
        email => 'xxxx@xxxx.com',
        pass => 'xxxxx',
    }
);
#my $array=$mech->forms();
#$mech->get('/home.php');
print Dumper($mech->forms());
#$mech->form_id("u_0_w");
$mech->submit_form(
  fields => {
      xhpc_message_text=>'Why so serious'
    }
);
print $response->status_line;
open($out, ">",  "output_page.html") or die "Can't open output_page.html: $!";
print $out $response->decoded_content;

Then I tried to print all the forms on the page using Dumper the output is:

$VAR1 = bless( {
             'default_charset' => 'UTF-8',
             'enctype' => 'application/x-www-form-urlencoded',
             'accept_charset' => 'UNKNOWN',
             'action' => bless( do{\(my $o = 'https://www.facebook.com/search/web/direct_search.php')}, 'URI::https' ),
             'method' => 'GET',
             'attr' => {
                         'method' => 'get'
                       },
             'inputs' => [
                           bless( {
                                    'tabindex' => '-1',
                                    'value' => '1',
                                    'class' => '_42ft _42fu _4w98',
                                    'type' => 'submit'
                                  }, 'HTML::Form::SubmitInput' ),
                           bless( {
                                    '/' => '/',
                                    'autocomplete' => 'off',
                                    'tabindex' => '1',
                                    'name' => 'q',
                                    'aria-label' => 'Search Facebook',
                                    'value_name' => '',
                                    'class' => 'inputtext _586f',
                                    'type' => 'text',
                                    'id' => 'u_0_b',
                                    'role' => 'combobox',
                                    'placeholder' => 'Search Facebook'
                                  }, 'HTML::Form::TextInput' )
                         ]
           }, 'HTML::Form' );

It means it is not detecting the status update form it is detecting only Facebook search form.

  1. What may be the problem for mechanize not detecting all the form elements?
  2. The form contains <button type="submit">. Do Mechanize support it?
simbabque
  • 53,749
  • 8
  • 73
  • 136
Arunesh Singh
  • 3,489
  • 18
  • 26
  • 3
    Maybe you should look into the APIs that facebook provides. Using screenscraping/automation like this might be against their ToS. – simbabque Jul 09 '15 at 13:23
  • @simbabque yes sorry it is`form_id`. I not abusing it ,just curious why it is not working. – Arunesh Singh Jul 09 '15 at 13:33
  • 1
    Not _might be_, it __is__ against their ToS. And they are taking active measures against it, so that might already be why it is “nor working”. // If you want to do anything “on” Facebook, use their API. – CBroe Jul 09 '15 at 15:02

1 Answers1

0

Why do you have to use Mechanize for this? There's already a module available for this on CPAN.

Take a look at WWW::Facebook::API.

Also see a related question: How do I use Perl's WWW::Facebook::API to publish to a user's newsfeed?

Synopsis:

use WWW::Facebook::API;
my $facebook = WWW::Facebook::API->new(
    desktop => 0,
    api_key => $fb_api_key,
    secret => $fb_secret,
    session_key => $query->cookie($fb_api_key.'_session_key'),
    session_expires => $query->cookie($fb_api_key.'_expires'),
    session_uid => $query->cookie($fb_api_key.'_user')
);

my $response = $facebook->stream->publish(
 message => qq|Test status message|,
);
Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133