MATLAB实现写谷歌翻译的小爬虫
生活随笔
收集整理的這篇文章主要介紹了
MATLAB实现写谷歌翻译的小爬虫
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文主要就是對(duì)比用python寫(xiě)的一個(gè)谷歌翻譯的爬蟲(chóng),分享一下MATLAB代碼,主要使用的函數(shù)就是urlread和regexp。
先看一下效果:
代碼是這個(gè)樣子的(兩個(gè)文件一個(gè)主文件:Google_translate.m)一個(gè)函數(shù)文件Translate_mean.m:我用的全局變量傳遞參數(shù)所以都沒(méi)有輸入。這個(gè)是主函數(shù)Google_translate.m的內(nèi)容:
function Google_translateglobal Source_first Translate_two Source_Content Translate_Content global Langurage fh = figure('name','Google Translate');Langurage = {'zh-CN','en','ja','fr','de'};Source_first = uicontrol('parent', fh, 'style', 'popupmenu',...'units','normalized',...'position',[0.4, 0.7 0.1, 0.1],...'string',Langurage);Translate_two = uicontrol('parent', fh, 'style', 'popupmenu',...'units','normalized',...'position',[0.55, 0.7 0.1, 0.1],...'string',Langurage);Source_Content = uicontrol('parent', fh, 'style', 'edit',...'units','normalized',...'position',[0.1, 0.1, 0.4, 0.6],...'HorizontalAlignment','left',...'Max',2); % set( Source_Content )Translate_Content = uicontrol('parent', fh, 'style', 'edit',...'units','normalized',...'position',[0.55, 0.1, 0.4, 0.6],...'HorizontalAlignment','left',...'BackgroundColor',[1,1,1],...'Max',2); % set( Translate_Content )Translate = uicontrol('parent', fh, 'style', 'pushbutton',...'units','normalized',...'position',[0.1, 0.74, 0.2, 0.07],...'string','Translate',...'callback','Translate_mean');uicontrol('parent',fh, 'style','text','units','normalized',...'position',[0.23, 0.84, 0.6, 0.1],...'string','Google translation API using',...'fontsize',18)這個(gè)是button調(diào)用的小函數(shù)Translate_mean.m的內(nèi)容:
function Translate_mean %{ Source = 'en'; Totrans = 'cn'; ANS_url = urlread(['http://translate.google.cn/translate_a/single?client=gtx&sl=',Source, '&tl=', Totrans, '&dt=t&q=googleq=how are you']); %} global Source_first Translate_two Source_Content Translate_Content global LangurageH1 = get(Source_first, 'value'); H2 = get(Translate_two, 'value'); Content_source = get(Source_Content,'string');Source = Langurage{H1}; Totrans = Langurage{H2}; Content = Content_source;Content(Content=='。') = '.';LEN = size(Content,1); TransContent = cell(LEN,1); for ihang = 1:LEN% string to UTF-8Str2 = dec2hex( unicode2native(Content(ihang,:), 'UTF-8') );Allstr = [];for i = 1:length(Str2)Allstr = [ Allstr,'%', Str2(i,:) ];endANS_url = urlread(['http://translate.google.cn/translate_a/single?client=gtx&sl=',...Source, '&tl=', Totrans, '&dt=t&q=googleq=', Allstr, '&ie=UTF-8']);Tl = regexp( ANS_url, '[[["[gG]oogle.*u003d (.*)","google', 'tokens' );TransContent{ihang} = Tl{1,1}{1,1}; endset(Translate_Content, 'string', TransContent)?
以上就是如何實(shí)現(xiàn)最開(kāi)始的效果圖的哪個(gè)功能。我下面給一個(gè)小代碼是用來(lái)普通測(cè)試,測(cè)試過(guò)了之后就可以再去調(diào)試上面兩個(gè)小函數(shù):(有一點(diǎn)注釋講究著看吧)
clear;clc Source = 'zh-CN'; % Source = 'en'; Totrans = 'en'; Str = '你是個(gè)沙雕額';% string to UTF-8 Str2 = dec2hex( unicode2native(Str, 'UTF-8') ); Allstr = []; for i = 1:length(Str2)Allstr = [ Allstr,'%', Str2(i,:), ]; end% Str = '%E6%B1%89';% sl = source language % tl = translate language % ie = input format % oe = output format ANS_urlr = urlread(['http://translate.google.cn/translate_a/single?client=gtx&sl=',...Source, '&tl=', Totrans, '&dt=t&q=googleq=',Allstr,'&ie=UTF-8']);Tl = regexp( ANS_urlr, '[[["[gG]oogle.*u003d (.*)","google', 'tokens' ); result = Tl{1,1}{1,1} % Tl{1,1}{1,1}% &oe=UTF-8 % &ie=UTF-8?
MATLAB的爬蟲(chóng)比python的似乎要方便。
就是感覺(jué)很粗暴,直接一個(gè)網(wǎng)址。但是想了想簡(jiǎn)單的東西一般功能就少一點(diǎn)吧。
?
總結(jié)
以上是生活随笔為你收集整理的MATLAB实现写谷歌翻译的小爬虫的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cocos2dx各个版本下载地址
- 下一篇: 交大计算机专硕研究生经验贴,交大软件工程