I have the following fairly simple script to parse BookMooch data:
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp;
use WWW::Mechanize;
use XML::Simple;
use YAML;
use Encode;
my $mech = WWW::Mechanize->new;
my $api_base = 'http://api.bookmooch.com/api/userid';
my $q = CGI->new;
my $user = $q->param('user');
$mech->get("$api_base?userids=$user");
die "Failed to get user $user from BookMooch"
unless $mech->success;
print header, start_html, "\n" ;
my $xml = $mech->content;
# $xml = encode('iso-8859-1', $xml); (doesn't fix the problem)
my $data = XMLin($xml);
print pre(Dump($data)), end_html;
When I run it, I get the following output:
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U
+S">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+" />
</head>
<body>
:1492: parser error : Input is not proper UTF-8, indicate encoding !
Bytes: 0xA3 0x31 0x20 0x31
<condition>Good condition. 1970 edition with n
+et cover price shown as 1 15s
+ ^
at /usr/lib/perl5/XML/LibXML/SAX/Parser.pm line 31
Uncommenting the line with encode, doesn't make any difference. I'm new to UTF-8 and encoding. What's the correct incantation for what I'm doing?
The input data is Latin-1 as far as I'm aware, and it's b0rking on a pound sign '£'
Any help would be much appreciated
-
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.