Perl Automatic Webpage processing WordPress Automatic Login View

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.