MATLAB读取网页内链接,Matlab使用urlread()读取网页乱码问题

2021年2月9日发(作者:television是什么意思)

Matlab

使用

urlread()

读取网页乱码问题

事先声明这是本人在网上找到的(

/

2012-05/

)

,非

原创:

Matlab

urlread()

函数可以读取网页,调用语法:<

p>

S =

urlread('URL','method',PARAMS)

共有三个参数,第

一个是网页地址,第二个是

get

或是

post

,意思很直白;第三个则是要向

网页传递的参数,详细

help

文档。本博文的这个问题不是重点。

重点在于,用这个读取中文网页会乱码。例如:

S=

urlread('');

自己去看

S

的内容,里面凡是应该是中文文字的部分都是问号。

<

/p>

3

年前,我写

Matlab

基础班讲义的时候实际已经把这个问题解决了,但是没有记录下来,

这次又遇

上同样的问题,一下子记不起来了(翻开当年讲义,看到自己写到

这个问题可以

通过手工修改

urlread()<

p>

函数解决

,感到很愤慨,多写一句我就不用费那

劳什子的精神了)

废话打住,<

/p>

下面开工修理

urlread()

函数,

让它能够正常搞中文网页

(注我用的是

2012A

版本,

其他版本可能有变化)

。在

Matlab

中输入

edit

urlread

打开函数代码文件。

修改

1

修改第一行的函数定义,本来是三个参数,在最后添加一个。

例如,我修改后:

function

[output,status] =

urlread(urlChar,method,params,webencoding)

在下面添加一句

if

nargin<4; webencoding='UTF-8'; end

作用是与修改前的函数调用语法兼容。

修改

2

<

/p>

修改参数数量检验语句,该语句位于文件中偏前部,例如原来是

n

arginchk(1,3)

na

rginchk(1,4)

修改

3

修改文件尾部的数据转换语句,它原本是:

output =

native2unicode(typecast(Array','uint8'),'UTF-8');

webencoding

替换

'UTF-8'

,注意包括单引号也要替换。

搞定了,

然后再读取网页。

一般中文网

页的网页编码是

'GBK'

(

兼容

p>

’GB2312'

)

调用语法是:

S=urlread('','get','','GBK')

这样返回来的文字里面就可以正常显示中文了。

为了方便大家修改,我把改好后的代码和未改的原代码

放在了下一页,就这一点是比原文

多的,也是害怕自己忘了。要是嫌麻

烦,就把下一页的代码复制粘贴过去,覆盖源代码,不

过别忘了把原代码保留一份,免得

忘了怎么改回来。

修改后的代码:

function [output,status] =

urlread(urlChar,method,params,webencoding)

if nargin<4;

webencoding='UTF-8';

end

%URLREAD Returns the contents of a URL

as a string.

%

S = URLREAD('URL') reads the content at

a URL into a string, S.

If

the

%

server returns binary data, the string

will contain garbage.

%

%

S =

URLREAD('URL','method',PARAMS) passes information

to the server as

%

part of the request.

The 'method' can be 'get',

or 'post' and PARAMS is a

%

cell array of

param/value pairs.

%

%

[S,STATUS] =

URLREAD(...) catches any errors and returns 1 if

the file

%

downloaded successfully and 0

otherwise.

%

%

Examples:

%

s = urlread('')

%

s

= urlread('ftp:///README')

%

s =

urlread(['file:///'

fullfile(prefdir,'history.m')])

%

%

From behind a firewall, use the

Preferences to set your proxy server.

%

%

See also URLWRITE.

%

Matthew J. Simoneau, 13-Nov-2001

%

Copyright 1984-2008 The MathWorks, Inc.

%

$$Revision: 1.3.2.10 $$ $$Date: 2008/10/02

18:59:57 $$

% This function

requires Java.

if ~usejava('jvm')

error('MATLAB:urlread:NoJvm','URLREAD

requires Java.');

end

import uptibleStreamCopier;

% Be sure the proxy settings are set.

xySettings

%

Check number of inputs and outputs.

error(nargchk(1,4,nargin))

error(nargoutchk(0,2,nargout))

if ~ischar(urlChar)

err

or('MATLAB:urlread:InvalidInput','The first input,

the URL, must be a character array.');

end

if (nargin > 1) &&

~strcmpi(method,'get') && ~strcmpi(method,'post')

p>

error('MATLAB:urlread:InvalidInput','Seco

nd argument must be either

end

% Do we want to throw

errors or catch them?

if nargout == 2

catchErrors = true;

else

catchErrors = false;

end

% Set default outputs.

output = '';

status = 0;

% GET method.

Tack param/value to end of URL.

if (nargin > 1) &&

strcmpi(method,'get')

if

mod(length(params),2) == 1

error('MATLAB:u

rlread:InvalidInput','Invalid parameter/value pair

arguments.');

end

for

i=1:2:length(params)

if (i == 1),

separator = '?'; else separator = '&'; end

param = char((params{i}));

value = char((params{i+1}));

urlChar = [urlChar separator param '='

value];

end

end

% Create a urlConnection.

[urlConnection,errorid,errormsg] =

urlreadwrite(mfilename,urlChar);

if

isempty(urlConnection)

if catchErrors,

return

else

error(errorid,errormsg);

end

end

% POST

method.

Write param/values

to server.

if (nargin > 1) &&

strcmpi(method,'post')

try

utput(true);

uestProperty(

...

'Content-

Type','application/x-www-form-urlencoded');

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值