How to solve the garbled problem in remote page crawl

Source: Internet
Author: User
Tags return window
Solve | problem | page

Because the usual use of the Web page files are basically gb2312 encoding, if through the XMLHTTP control read, there will be garbled problems, but to all the files are changed to Utf-8 code, the operation is more troublesome. After repeated comparisons, I found a relatively practical solution.
FF provides a Overridemimetype method that specifies the encoding of the return value, so it is simpler to solve, as long as you point this method at gb2312. For IE, you can use a VBS fit to encode the conversion, using Gb2utf8 to transform the received binary data, which uses the Execscript method to invoke the function of the VBS.


[Copy this Code] CODE:
<body></body>
<script>
LoadPage ("index.htm")
function loadpage (URL) {
var Xh=window. Event?new XMLHttpRequest (): New ActiveXObject ("Microsoft.XMLHTTP")
Xh.open ("Get", url,true)
if (window. Event)
Xh.overridemimetype ("text/xml;charset=gb2312")
Xh.onreadystatechange=function () {
if (xh.readystate!=4)
Return
var V
V=window. Event?xh.responsetext:window.gb2utf8 (Xh.responsebody)
Alert (v)
}
Window.gb2utf8=function (data) {
var Glbencode=[],t,i,j,len
Gb2utf8_data=data
Execscript ("Gb2utf8_data = MidB (gb2utf8_data, 1) +" ", VBScript")
T=escape (gb2utf8_data). Replace (/%u/g, ""). Replace (/(. { 2}) (. { 2})/g, "%$2%$1"). Replace (/% [A-z].)% (. {2}) /g, "@$1$2")
T=t.split ("@")
I=0
Len=t.length
while (++i<len) {
J=t[i].substring (0,4)
if (!glbencode[j]) {
Gb2utf8_char = eval ("0x" +j)
Execscript ("GB2UTF8_CHAR=CHR (Gb2utf8_char)", "VBScript")
Glbencode[j]=escape (Gb2utf8_char). substring (1,6)
}
T[i]=glbencode[j]+t[i].substring (4)
}
Gb2utf8_data=gb2utf8_char=null
Return unescape (T.join ("%")). Slice (0,-1)
}
Xh.send (NULL)
}
</script>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.