MATLAB实现写谷歌翻译的小爬虫
生活随笔
收集整理的這篇文章主要介紹了
MATLAB实现写谷歌翻译的小爬虫
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文主要就是對比用python寫的一個谷歌翻譯的爬蟲,分享一下MATLAB代碼,主要使用的函數就是urlread和regexp。
先看一下效果:
代碼是這個樣子的(兩個文件一個主文件:Google_translate.m)一個函數文件Translate_mean.m:我用的全局變量傳遞參數所以都沒有輸入。這個是主函數Google_translate.m的內容:
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)這個是button調用的小函數Translate_mean.m的內容:
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)?
以上就是如何實現最開始的效果圖的哪個功能。我下面給一個小代碼是用來普通測試,測試過了之后就可以再去調試上面兩個小函數:(有一點注釋講究著看吧)
clear;clc Source = 'zh-CN'; % Source = 'en'; Totrans = 'en'; Str = '你是個沙雕額';% 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的爬蟲比python的似乎要方便。
就是感覺很粗暴,直接一個網址。但是想了想簡單的東西一般功能就少一點吧。
?
總結
以上是生活随笔為你收集整理的MATLAB实现写谷歌翻译的小爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocos2dx各个版本下载地址
- 下一篇: 交大计算机专硕研究生经验贴,交大软件工程