I am fetching some pages over the Web using Perl's LWP::UserAgent
and would like to be as polite as possible. By default, LWP::UserAgent
does not seamlessly handle compressed content via gzip. Is there an easy way to make it do so, to save everyone some bandwidth?
相关问题
- $ENV{$variable} in perl
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Change first key of multi-dimensional Hash in perl
- How do I get a filehandle from the command line?
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- What is a good way to deploy a Perl application?
- Speeding up Selenium Webdriver
LWP has this capability built in, thanks to
HTTP::Message
. But it's a bit hidden.First make sure you have
Compress::Zlib
installed so you can handlegzip
.HTTP::Message::decodable()
will output a list of allowed encodings based on the modules you have installed; in scalar context, this output takes the form a comma-delineated string that you can use with the 'Accept-Encoding
' HTTP header, whichLWP
requires you to add to yourHTTP::Request
-s yourself. (On my system, withCompress::Zlib
installed, the list is "gzip
,x-gzip
,deflate
".)When your
HTTP::Response
comes back, be sure to access the content with$response->decoded_content
instead of$response->content
.In
LWP::UserAgent
, it all comes together like this:This will also decode text to Perl's unicode strings. If you only want
LWP
to uncompress the response, and not mess with the text, do like so: