Skip to content

Encoding Error #78

Closed
Closed

Description

@jensenbyte
var dom = CQ.CreateFromUrl("http://www.cnbeta.com/articles/222432.htm");
var node = dom.Select("#news");
string html = HttpUtility.HtmlDecode(node.Html());

it's looks like the logic of get encoding has some problem.
cannot just set the default encoding to ISO-8859-1.

public static Encoding GetEncoding(IHttpWebResponse response)
    {
        return String.IsNullOrEmpty(response.CharacterSet) ?
           Encoding.GetEncoding("ISO-8859-1") :
           Encoding.GetEncoding(response.CharacterSet);

    }

i had tried to fix it, but the sourcecodes cannot pass buliding.

Error 1 Source file 'HtmlParserSharp\HtmlParserSharp\Common\Attributes.cs' could not be found D:\Test Projects\CsQuery-master\source\CsQuery\CSC CsQuery

...
all of HtmlParserSharp\ sourcecodes...

Activity

  1. jensenbyte commented on Jan 16, 2013

    @jensenbyte
    Author

    my sugesstion:

    1. the CQ.CreateFromUrl() need add a encoding param.
    2. auto detect Encoding from bytes

    you can ref the HttpClient or HtmlAgilityPack.

    hope the team doing well.

  2. jamietre commented on Jan 16, 2013

    @jamietre
    Owner

    Are you saying that "ISO-8859-1" is not a valid encoding in your environment?

    Also btw - the compiling problem is because HtmlParserSharp is a submodule, it needs to be initialized separately - there are instructions in the main readme.

  3. jamietre commented on Jan 16, 2013

    @jamietre
    Owner

    OK I think I fixed this in the last push. So now the character set will be detected in the following way:

    1. From the mime type of the HTTP response
    2. From an http-equiv tag
    3. If neither are present from the BOM
    4. If there is no BOM, then UTF8

    By the way I don't want to add an encoding parameter for CreateFromUrl because I am not sure how useful it is. You really don't know what the encoding of a remote resource is until you access it and inspect the headers and the stream. Also, you can always use Create(Stream stream, Encoding encoding) if you want to specify an arbitrary encoding, and get the stream yourself

  4. jensenbyte commented on Jan 17, 2013

    @jensenbyte
    Author

    i agree with your most mind.

    but the problem is the mime type and http-equiv tag are not reliable.

    only the stream and bytes are reliable to detect the right encoding.

  5. jamietre commented on Jan 17, 2013

    @jamietre
    Owner

    Are you saying that if valid byte order marks are found then the mime type and http-equiv should be ignored?

    Btw did you try the update? I think the problem you were having is just my incorrect use of the latin-1 charset. If the character set requested by the headers is not available it will still fall back on the BOM or UTF8.

  6. jensenbyte commented on Jan 18, 2013

    @jensenbyte
    Author

    yes, i think the encoding info from http-equiv or http header maybe isn't match to the encoding of contents.

    I got the new sourcecode, it's ok now.
    well done. thanks you very much.

    my os: win8 english, system locale: chinese(simplified, china)

  7. jamietre commented on Jan 18, 2013

    @jamietre
    Owner

    OK - awesome, glad it worked. There have been a lot of changes to the character set handling in the last couple weeks based on feedback from you and a few others, I hope to get this into a release soon.

  8. jamietre commented on Feb 6, 2013

    @jamietre
    Owner

    In release 1.3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions