scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.52k stars 345 forks source link

Is there any way to get the html encoding not working properly? #159

Closed yoonjason closed 4 years ago

yoonjason commented 4 years ago

hello Is there any way to get the html encoding not working properly?

it's url "http://www.11st.co.kr/product/SellerProductDetail.tmall?method=getSellerProductDetail&prdNo=2736133462"

this method

urlTextField.text = "http://www.11st.co.kr/product/SellerProductDetail.tmall?method=getSellerProductDetail&prdNo=2736133462"

do {
//            let html: String = try String(contentsOf: URL(string: urlTextField.text!)!, encoding: .utf16)
            let html = try String(contentsOf: URL(string: urlTextField.text!)!)
            let doc: Document = try SwiftSoup.parse(html)
            let metaImage: Element? = try doc.head()?.select("meta[property=og:image]").first()
            let metaTitle: Element? = try doc.head()?.select("meta[property=og:title]").first()

            let image: String? = try metaImage?.attr("content")
            let title: String? = try metaTitle?.attr("content")
            let html2 = try String.init(contentsOf:URL(string: urlTextField.text!)!)
            let doc2: Document = try SwiftSoup.parse(html)
            print(doc2)
        } catch Exception.Error(let type, let message) {
            print(message)
            print(type)
        } catch  {
            print("error")
        }

error message Error Domain=NSCocoaErrorDomain Code=264 "The file “SellerProductDetail.tmall” couldn’t be opened because the text encoding of its contents can’t be determined.

When importing html, even if utf8, utf16, and encoding options were not set, html could not be imported.

let html: String = try String(contentsOf: URL(string: urlTextField.text!)!, encoding: .utf8)
let html: String = try String(contentsOf: URL(string: urlTextField.text!)!, encoding: .utf16)
let html: String = try String(contentsOf: URL(string: urlTextField.text!)!, encoding: .utf32)
let html: String = try String(contentsOf: URL(string: urlTextField.text!)!)

all not working

but, i searched encoding : ascii, japaneseeuc~~ working. text is Chinese;; but, url is korean site. data is not korean...

How can I get the normal data?

scinfu commented 4 years ago

HI, this is an error of foundation Apple framework not SwiftSoup, try to see this issue #150

yoonjason commented 4 years ago

If you look at section 150, you said Try with a GET, which method should you use?