Ruby Error:"in `match':incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) (Encoding::CompatibilityError)" 发表于 2016-08-10 更新于 2021-11-17 分类于 Ruby 我在抓取一个中文网页时遇到该问题.出现该问题的原因是字符编码不一致导致的.查看获取到的网页内容编码是 ASCII-8BIT的,所以需要强制转换为UTF-8. 1234code_html = HTTP.get('http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html').to_sputs code_html.encoding.nametmp_result = code_html.force_encoding("UTF-8").scan(/\d{6} [\u4e00-\u9fa5]+/)puts tmp_result