Reprinted site please note, reproduced from: Fu kai [http://www.php-oa.com]
Link: http://www.php-oa.com/2011/06/09/perl-web-lwp-cookie-wordpress-auto.html
Automated Processing and most applications on the Internet are based on the HTTP protocol. Originally, I used Perl for processing, and it was a bit messy. Later I found it very simple.
Step 1: briefly touch the basic method of the lwp: useragent Module
Step 2: Understand the following parts of the response: http: request, http: Response, http: headers, http: cookies.
Step 3: extract the webpage content and find that the modules in Perl are super powerful. Use the Web: scraper, which is the best choice.
The following is an example of my previous writing, that is, a small example of automatically logging in to my blog and taking out the latest comments. You can write a small blog software using this command line.
#! /Usr/bin/perluse strict; Use warnings; Use http: request; Use http: cookies; Use lwp: useragent; use web: scraper; my $ url = 'HTTP: // www.php-oa.com/wp-login.php'; # used to store cookiemy $ cookie_jar = http: cookies-> New (file => ". /cookies. lwp ", autosave => 1,); # Put the cookie processing object in lwp: useragent to process the cookie # log on to my $ UA = lwp: useragent-> new; my $ cookies = $ UA-> cookie_jar ($ cookie_jar); $ UA-> agent ('mozilla/6 [CN] (Ubuntu; Linux )'); my $ res = $ UA-> post ($ URL, [log => 'admin', Pwd => 'passwd ', 'wp-submit '=> 'Log in',],); # Based on the redirected address, upload my $ redirect_to = $ res-> header ('location') to the original cookie; my $ Req = http: Request-> New (get => $ redirect_to ); $ cookie_jar-> add_cookie_header ($ req); my $ new_res = $ UA-> request ($ req ); # process the returned result response and retrieve the content my $ scraper = scraper {Process '/html/body/Div [2]/Div [2]/Div [2]/Div [2]/Div [2]/Div/BLOCKQUOTE/P ', 'comment [] '=> 'text';}; my $ result = $ scraper-> scrape ($ new_res-> content ); # output result my @ comments =@{$ result-> {comment }}; for (0 .. $ # comments) {print "$ _. $ comments [$ _] \ n ";}
Web: scraper is highly recommended. For details, refer to my previous articles:
[Perl] The best module for Web Analysis and Processing. Web: Scraper
We recommend that you learn reference: http://www.perl.com/pub/2002/08/20/perlandlwp.html