在java中存储和解析HTML时特殊字符和符号的问题

Bou*_*ter 2 java selenium special-characters

我正在使用Selenium来测试电子商务应用程序.我需要检查在列表页面上选择类别时列出的项目是否与数据库中的项目匹配.所以我使用selenium访问页面并将页面源存储在文本文件中.我稍后使用HTMLCleaner和JSoup解析此文本文件以获取我希望用DB验证的字段.

但是,我注意到页面上列出的某些产品使用特殊字符,如™,®等,这些字符未正确存储/检索并显示为问号.

我用来存储页面源代码:

BufferedWriter writer = null;
try
{
    writer = new BufferedWriter(new FileWriter(filepath+"/"+filename+".txt"));
    writer.write(driver.getPageSource());
}
catch ( IOException e)
{
    e.printStackTrace();
}
finally
{
    try
    {
        writer.close( );
    }
    catch (IOException e)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

检索和解析文件

Document htmlFile = Jsoup.parse(fileSavedPreviously,"ISO-8859-1");
TagNode tagNode = new HtmlCleaner().clean(fileSavedPreviously);

try {
    org.w3c.dom.Document doc = new DomSerializer(new CleanerProperties())
        .createDOM(tagNode);

} catch (ParserConfigurationException e) {
    e.printStackTrace();
}

//rest of the parsing....
Run Code Online (Sandbox Code Playgroud)

Flo*_* B. 5

定义流编写器的编码:

writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullpath), "UTF-8"));
Run Code Online (Sandbox Code Playgroud)

并为解析器提供相同的一个:

Document htmlFile = Jsoup.parse(fileSavedPreviously, "UTF-8");
Run Code Online (Sandbox Code Playgroud)

要获取页面的编码,请document.inputEncoding在浏览器控制台中执行.