远程抓取GOOGLE的自动翻译结果

 1 ExpandedBlockStart.gif ContractedBlock.gif          Function RemoveHTML() Function RemoveHTML(strhtml)
 2InBlock.gif            if strhtml<>"" then
 3InBlock.gif                 dim s_str as string
 4InBlock.gif                     s_str=regex.replace(strHTML,"<[^>]+>","")
 5InBlock.gif                     s_str=replace(s_str,"&nbsp;","")
 6InBlock.gif                     return s_str
 7InBlock.gif            end if
 8ExpandedBlockEnd.gif        End Function

 9 None.gif        
10 ExpandedBlockStart.gifContractedBlock.gif         Function china_to_english() Function china_to_english(texts,languages)
11InBlock.gif            Dim payload As String = "hl=zh-CN&ie=UTF8&text="& texts &"&langpair="& languages &""
12InBlock.gif            dim str_return as string  
13InBlock.gif            Dim req As WebRequest = WebRequest.Create("http://translate.google.com/translate_t"' 开始取连接. 
14InBlock.gif            req.Credentials = CredentialCache.DefaultCredentials       '取得默认
15InBlock.gif            req.Method = "POST"                                        '以POST方式发送,这里默认是以GET方式发送
16InBlock.gif            req.ContentType = "application/x-www-form-urlencoded"      'POST方式需在传送这个编码,如果上传文件,则修为multipart/form-data
17InBlock.gif            req.timeout=10000                                          '连接超时定时
18InBlock.gif            req.ContentLength = payload.Length                         '头部长度
19InBlock.gif            Dim encoding As Encoding = Encoding.GetEncoding("UTF-8")   '转换成流,大部网站一般转换成UTF-8就可以了,注意是大写的编码
20InBlock.gif            Dim bytes As Byte() = Encoding.GetBytes(payload)           '转换成流
21InBlock.gif            req.ContentLength = bytes.Length                           '传送流的长度
22InBlock.gif            Dim newStream As Stream = req.GetRequestStream()           '转换写入
23InBlock.gif            newStream.Write(bytes, 0, bytes.Length)                    '写入传送流
24InBlock.gif            newStream.Close()                                          '关闭
25InBlock.gif            '上面发送完成,下面取得服务器返回 
26InBlock.gif            Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)  ' 传递返回标识
27InBlock.gif            if res.StatusDescription="ok" then  ' 返回取得状态.
28InBlock.gif               current.response.write("暂时无法连接到网站,请换用另一个程序")
29InBlock.gif               current.response.end()
30InBlock.gif            end if
31InBlock.gif            Dim dataStream As Stream = res.GetResponseStream()         ' 返回给指针
32InBlock.gif            Dim reader As New StreamReader(dataStream,encoding.getencoding("gb2312")) ' 读
33InBlock.gif            Dim responseFromServer As String = reader.ReadToEnd()      ' 读取所有
34InBlock.gif            
35InBlock.gif            str_return=responseFromServer                              '赋值回传
36InBlock.gif            
37InBlock.gif            reader.Close()                                             '接下来三个关闭
38InBlock.gif            dataStream.Close()
39InBlock.gif            res.Close()
40InBlock.gif
41InBlock.gif            dim ss as string = str_return
42InBlock.gif            ss = regex.replace(ss,"(?i:(.+)(\<div)(.+)(\>)(.+)(\<\/div\>)(.+))","$5"'提取我们所要的译文
43InBlock.gif            ss = RemoveHTML(ss)                                       '删除HTML
44InBlock.gif            ss = ss.substring(3)                                      '删除翻译二字
45InBlock.gif            return ss                                                 '函数返回
46ExpandedBlockEnd.gif    End function

47 None.gif        
48 None.gif
    在调用china_to_english(texts,languages)需要传两个参数第一个为要译的文字,第二个是要进行相对译的语种代码.
   例:中译英,其第二个参数为:zh-Cn|en
   我导入的命名空间如下:
Imports System
Imports System.Web
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Web.HttpContext
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Net

转载于:https://www.cnblogs.com/qtybb/archive/2006/10/26/540413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值