Perl在命令提示符中无法打印特殊字符

3

您好,我希望能在命令提示符和HTML文件中打印结果。我在HTML中使用编码(cp1252)进行打印,但是在命令提示符中无法看到那些特殊字符,而是得到一些乱码值。例如,“£”被打印为“ú”。谢谢。

use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder::XPath;
use LWP::UserAgent;

my $competitor_declare='7shop';
my $xpath_declare='//strong';
my @urls = ("http://www.7dayshop.com/delivery-and-returns"); 


open HTML1, '>:encoding(cp1252)',"C:/Users/jeyakuma/Desktop/$competitor_declare.html";
open HTML, '>:encoding(cp1252)',"C:/Users/jeyakuma/Desktop/shipping project/database/$competitor_declare.html";  


foreach my $url (@urls)
        {
        print "\n\nworking on $url\n\n";
        my $ua = LWP::UserAgent->new( agent => "Mozilla/5.0" );
        my $req = HTTP::Request->new( GET => "$url" );
        my $res = $ua->request($req);

        if ( $res->is_success ) 
        {
           print "Please wait while we create file \n\n";
            my $xp = HTML::TreeBuilder::XPath->new_from_url($url);
           my $node = $xp->findnodes_as_string("$xpath_declare") or print "couldn't find the node\n"; #give xpath
            print HTML1 $node and print "Dump file is created please configure the same in xpathconfiguration.pl\n" and print HTML $node;
            print $node;
        }
        else{  
                print "file creation failed\n";

        }
}

在命令提示符中预期的输出

cost is - £1.99

当前结果

cost is - ú1.99

你的终端设置为什么编码? - Hunter McMillen
当您显示HTML文件时,您是否看到了这个?请检查文件中使用的字符代码。还可以尝试 print "£££\n" 并查看屏幕上出现了什么。 - Borodin
嗨,感谢在HTML中的支持。我可以看到“成本为-£1.99”,但在Ubuntu终端中打印为“成本为-�1.99”。我在编写这些HTML时使用了cp1252进行编码。 - Jeya Kumar
1个回答

4

7dayshop使用utf8字符集:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

在您的Windows计算机上,有两个半步骤需要执行,以便在控制台中读取utf8

  1. Modify your encoding of STDOUT using the following:

    binmode STDOUT, ':utf8:raw';
    
  2. Change the encoding of your console using the following command before running your script:

    chcp 65001
    
  3. You might need to edit the font of your console to one like Lucida Console.

下面展示了我在Windows机器上的输出:
use strict;
use warnings;
use autodie;

use LWP::Simple;
use HTML::TreeBuilder::XPath;
use LWP::UserAgent;

binmode STDOUT, ':utf8:raw';

my $competitor_declare = '7shop';
my $xpath_declare      = '//strong';
my @urls               = ("http://www.7dayshop.com/delivery-and-returns");

foreach my $url (@urls) {
    print "\n\nworking on $url\n\n";
    my $ua = LWP::UserAgent->new( agent => "Mozilla/5.0" );
    my $req = HTTP::Request->new( GET => "$url" );
    my $res = $ua->request($req);

    if ( $res->is_success ) {
        print "Please wait while we create file \n\n";
        my $xp = HTML::TreeBuilder::XPath->new_from_url($url);
        my $node = $xp->findnodes_as_string("$xpath_declare") or print "couldn't find the node\n";    #give xpath
        print $node;
    }
    else {
        print "file creation failed\n";
    }
}

输出:

working on http://www.7dayshop.com/delivery-and-returns

Please wait while we create file

Dump file is created please configure the same in xpathconfiguration.pl
<strong>JavaScript seem to be disabled in your browser.</strong>
<strong>7DAYSHOP.COM</strong>
<strong>Get weekly special offers and new product news</strong>
<strong id="cartHeader"><span class="hide" id="basket-btn">My Basket</span> <span class="number">(<span>0</span>)</span></strong>
<strong>UK Mainland, Highlands &amp; Islands,</strong>
<strong>Ireland (ROI) &amp; select European destinations</strong>
<strong>Deliveries to UK</strong>
<strong>UK Mainland Standard - £1.99</strong>
<strong>UK Mainland Standard Tracked - £2.99</strong>
<strong>UK Mainland Express Tracked - £3.99</strong>
<strong>UK Mainland DPD Express Courier - £5.99</strong>
<strong>Deliveries to Highlands and Islands and Channel Islands</strong>
<strong>Highlands and Islands Standard - £1.99</strong>
<strong>Channel Islands Standard - £1.99</strong>
<strong>Highlands and Islands Express Tracked - £3.99 (Not Channel Islands)<br /></strong>
<strong>Highlands and Islands DPD Express Courier - £14.99</strong>
<strong>Channel Islands DPD Express Courier - £14.99</strong>
<strong>Deliveries to Ireland (ROI)</strong>
<strong>Ireland Standard - £4.99</strong>
<strong>Ireland DPD Express Courier - £14.99</strong>
<strong>Deliveries to France (FR)</strong>
<strong>France Standard - £1.99</strong>
<strong>France DPD Express Courier - £8.49</strong>
<strong>Deliveries to Germany (DE)</strong>
<strong>Germany Standard - £1.99</strong>
<strong>Germany DPD Express Courier - £6.49</strong>
<strong style="color: #000080;">Shipping Restrictions</strong>
<strong>All orders outside of the shipping restrictions will only be able to use our DPD Courier shipping service.</strong>
<strong style="color: #000080;"><br />Standard Delivery<br /></strong>
<strong>Oversized</strong>
<strong>Lithium</strong>
<strong><a href="http://www.7dayshop.com/lithium-batteries" target="_blank">Click here for further information about Lithium Battery deliveries.<br /><br /></a>Adhesives<br /></strong>
<strong><br /></strong>
<strong style="color: #000080;"><br />RETURNS / MISSING ITEMS</strong>
<strong>Returns Address:</strong>
ress:</strong>

嗨,米勒,感谢你的帮助。它完美地运行了。我能在Ubuntu 12.04中实现相同的功能吗? - Jeya Kumar
步骤2和3是特定于Windows的(据我所知)。然而,是的,我确定在Ubuntu上显示utf8字符是可能的,但我不熟悉如何操作。 - Miller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接