Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在bash中将标准输出转换为utf8_Mysql_Bash_Curl_Utf 8_Character Encoding - Fatal编程技术网

Mysql 在bash中将标准输出转换为utf8

Mysql 在bash中将标准输出转换为utf8,mysql,bash,curl,utf-8,character-encoding,Mysql,Bash,Curl,Utf 8,Character Encoding,我想将使用curl获得的任何网站的输出转换为utf8,用于数据库插入 用法示例: html=“$(curl-iL-compressed“$link”)” ##将非utf8$html转换为utf8所需的代码,最好不写入文件 ##用于插入的转义字符 html\u q=“${html/'\'\'/\\\\}” html\u q=“${html\u q/''/\'}” ##insert语句 sqlHtml='INSERT-INTO`''''${tableHtml}'`(`html`)值('''''${

我想将使用curl获得的任何网站的输出转换为utf8,用于数据库插入

用法示例:

html=“$(curl-iL-compressed“$link”)”
##将非utf8$html转换为utf8所需的代码,最好不写入文件
##用于插入的转义字符
html\u q=“${html/'\'\'/\\\\}”
html\u q=“${html\u q/''/\'}”
##insert语句
sqlHtml='INSERT-INTO`''''${tableHtml}'`(`html`)值('''''${html\u q}');'

mysql-u$dbUser-p$dbPass-h$dbHost-p$dbPort-D$dbName--default_character_set utf8-A简短问题,简短回答:

maniconv


现在,您还有一个问题:确定网页的源代码是什么。(提示:在google中键入charsetdetector)

在一般情况下,如果没有解析器,就无法正确执行此操作。脚本编写无法解决问题。如果您的目标是存储页面,请将其视为二进制文件,压缩并转换为可打印表单。

以下是我寻求的解决方案:

#/bin/bash
结果=“$({stdout=“$(curl-Lsv-compressed“$1”)”;}2>&1;echo”--分隔符--“echo”$stdout”);
回声'
发现:'
echo“$result”| grep-o'\(字符集\|编码\)[]*=[]*[“]*[a-zA-Z0-9:-]*'
回声“
状态=1
charset=“ISO_8859-1”#设置默认值
#1:HTTP内容类型:标头
#2:页面中的元素
#3:页面中的元素
正则表达式='.*(字符集编码)\s*=\s*[“]*([a-zA-Z0-9:-]*)”
如果[[“$result”=~$regex]]
然后
charset=“${BASH_重新匹配[2]}”
状态=2
echo“匹配成功:$charset”
其他的
echo“匹配失败:$charset:${BASH_重新匹配[2]}”
fi
如果[[“$charset”==*utf-8*| |“$charset”===*utf-8*]
然后
charset='NotModified'
其他的
将“iconv'$charset'回显到UTF-8//translatit”
html=$(echo“$result”| iconv-f“$charset”-t'UTF-8//translatit')
如果[$?-ne 0]
然后
echo“转换失败:iconv'$charset'到UTF-8//IGNORE”
html=$(echo“$result”| iconv-f“$charset”-t“UTF-8//IGNORE”)
如果[$?-ne 0]
然后
charset=“ISO_8859-1”
echo“忽略失败:iconv'$charset'到UTF-8//ignore”
html=$(echo“$result”| iconv-f“$charset”-t“UTF-8//IGNORE”)
状态=4
fi
状态=3
fi
fi
回显“字符集:'$charset',状态:'$status'”
默认为w3c。
它不是100%准确,但速度很快,99%的时间都能完成任务。

希望它能帮助处于同样情况的人。
也要感谢所有的回答。

您希望如何确定
$html
中数据的字符集是什么?您不能将随机垃圾转换为UTF-8并期望结果是合理的,您需要知道要从哪个字符集转换。可能使用curl的头,就像这样,这是bash而不是PHP。我不确定curl命令行客户机是否允许您轻松访问此标题。您可能会考虑在Python中编写此文档——请参阅可能的解决方案。不确定SQL表的外观,但如果您计划在未索引的页面中按内容搜索它们,则不比GREST更好。ICONV可以从STDUT执行吗?而不是来自文件?你是说来自stdin?如果是的话,是的。只需通过管道传输内容
which | iconv-fSOURCE\u ENCODING-tDEST\u ENCODING
define
parser
,请给出一个示例。在我的问题中,我想模拟浏览器输出的内容,使用该算法指定编码并将其转换为utf8。此外,如果我将其存储为二进制文件,如果我不知道编码,我将如何在其中搜索?了解html语法的软件可以将html正确地分割为不同的元素。web浏览器有一个,除非查看http内容头和/或html的元标记,否则您不知道页面的编码。我不知道您的目标是什么,如果您只是想要文本,请使用lynx之类的浏览器,将其作为格式化文本而不是curl转储。如果页面中没有1,2,3,并且存在数据内容,即“”,则iconv命令将失败。您不想将$result限制为可接受的有效子集吗?我可以添加如下内容:
如果[(iconv--list)]中的“${BASH_REMATCH[2]}”,那么charset=“${BASH_REMATCH[2]}”;否则保留默认值或尝试上一个匹配;fi
但这会减慢进程,我每天需要扫描超过10万个域。我将在后处理中处理这些错误,然后用更重的算法重新扫描它们。
html="$(curl -iL -compressed "$link")"

##code needed to convert nonUTF8 $html to utf8, preferably without writing to file

## escape characters for insert
html_q="${html//'\'/\\\\}"
html_q="${html_q//"'"/\'}"

## the insert statement
sqlHtml='INSERT INTO `'"${tableHtml}"'` (`html`) VALUES ('"'${html_q}'"');'
mysql -u$dbUser -p$dbPass -h$dbHost -P$dbPort -D$dbName --default_character_set utf8 -A <<ENDofMESSAGE
${sqlHtml}
ENDofMESSAGE
#!/bin/bash 

result="$( { stdout="$(curl -Lsv -compressed "$1")" ; } 2>&1; echo "--SePaRaToR--"; echo "$stdout")"; 
echo '
found:'
echo "$result" | grep -o '\(charset\|encoding\)[ ]*=[ ]*["]*[a-zA-Z0-9_: -]*'
echo ' '
status=1
charset="ISO_8859-1" #set default
# 1: HTTP Content-Type: header 
# 2: <meta> element in the page 
# 3: <xml> element in the page
regex='.*(charset|encoding)\s*=\s*["]*([a-zA-Z0-9_: -]*)'
if [[ "$result" =~ $regex  ]]
    then
        charset="${BASH_REMATCH[2]}"    
        status=2
        echo "match succes: $charset"
    else 
        echo "match fail: $charset : ${BASH_REMATCH[2]}" 
fi


if [[ "$charset" == *utf-8* || "$charset" == *UTF-8* ]]
    then
        charset='NotModified'
    else
    echo "iconv '$charset' to UTF-8//TRANSLIT"
    html=$(echo "$result" | iconv -f"$charset" -t'UTF-8//TRANSLIT')
    if [ $? -ne 0 ] 
        then
        echo "translit failed : iconv '$charset' to UTF-8//IGNORE"
        html=$(echo "$result" | iconv -f"$charset" -t"UTF-8//IGNORE")
        if [ $? -ne 0 ] 
            then            
            charset="ISO_8859-1"
            echo "ignore failed : iconv '$charset' to UTF-8//IGNORE"
            html=$(echo "$result" | iconv -f"$charset" -t"UTF-8//IGNORE")
            status=4
        fi
        status=3
    fi

fi
echo "charset: '$charset' , status: '$status'"