Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Fellow Monks,

I am trying to download the HTML of a URL which requires authentication (username and password). Using curl, I am able to successfully do this via:

curl --cookie "session=536...035a" -o "HTML_output.txt"

where the session ID is the one I obtained via the browser's developer tools function.

I am trying to replicate this using a more "native" Perl approach and using the LWP module, I have the following code which allows me to get the HTML of a given URL that does not require any authentication:

use LWP; my $url = ""; my $browser = LWP::UserAgent->new( ); my $resp = $browser->get($url); print $resp->content, "\n";

Is there a way using LWP (or some other module) to also include the session ID from the browser cookie?

Thank you in advance.

As a quick update, I have modified my code to use HTTP::Cookies::Netscape as follows:

use strict; use warnings; use LWP; use HTTP::Cookies::Netscape; my $url = ""; my $cookie_jar = HTTP::Cookies::Netscape->new( file => "/path/to/cookies.txt", ); my $browser = LWP::UserAgent->new( ); $browser->cookie_jar( $cookie_jar ); my $resp = $browser->get($url); print $resp->content, "\n";

Unfortunately, when I run that, the response that comes back tells me that I still need to supply a proper username and password, even though my session is still valid on the browser. I used the Firefox add-on "cookies.txt" to download a cookies.txt file which looks like:

# Netscape HTTP Cookie File TRUE / TRUE 1984932267 session 5361...1 +329

In reply to Using LWP (or some other module) to Dowload HTML with Cookie Session ID by Leudwinus

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2023-04-01 07:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found