最近在学习go,就用go写了一个简单的爬虫,解析结果时发现返回结果无法反序列化,仔细核对发现返回的结果跟python的不一样,go的结果是`"data":[{"info":[{"p1688":{"name":"珠宝品,"unit":"元/吨","value":"塑料行情"}}]`,python的结果是`"data":[{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}}],` 在“珠宝品”那三个字后面少了一个双引号,后面的内容也不一致。请问大家知道这是什么问题么。
具体代码如下
```go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
func main() {
code := "JO_112033"
page := 1
timeStamp := time.Now().UnixMilli()
url := "https://api.jijinhao.com/history/quotejs.htm?codes=%s&style=3¤tPage=%d&pageSize=3&_=%d"
url = fmt.Sprintf(url, code, page, timeStamp)
fmt.Println(url)
resp, _ := http.Get(url)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
```
go解析结果如下
```
var quot_str = [{"totalPage":"402","totalCount":"1206","data":[{"info":[{"p1688":{"name":"珠宝品,"unit":"元/吨","value":"塑料行情"}}],"quote":{"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒1":"8318.3300","q3":"8318.3300","q4":"8318.3300","q2":"8318.3300","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"-0.3793","q193":"1.0000","q70":"-31.6699","q63":"8318.3300","q59":"2022-10-18 00:00:00"}},{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨"e":"塑料行情"}}],"quote":{"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒报价","q1":"8350.000:"8350.0000","q4":"8350.0000","q2":"8350.0000","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"0.0000","q193":"1.0000","q70":"0.0000","q63":"8350.0000","q59":"2022-10-17 00:00:00"}},{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}}]"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒报价","q1":"8391.6700","q3":"8391.6700","q4":"1.6700","q2":"8391.6700","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"-0.4940","q193":"1.0000","q70":"-41.6602","q63":"8391.6700","q59":"2022-10-14 00:00:00"}}]}]
```
Python代码
```
from requests_html import HTMLSession
url = "https://api.jijinhao.com/history/quotejs.htm?codes=JO_112033&style=3¤tPage=1&pageSize=3&_=1666071223908"
res = HTMLSession().get(url)
print(res.text)
```
Python返回结果如下
```
var quot_str = [{"totalPage":"402","totalCount":"1206","data":[{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}}],"quote":{"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒报价","q1":"8318.3300","q3":"8318.3300","q4":"8318.3300","q2":"8318.3300","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"-0.3793","q193":"1.0000","q70":"-31.6699","q63":"8318.3300","q59":"2022-10-18 00:00:00"}},{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}}],"quote":{"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒报价","q1":"8350.0000","q3":"8350.0000","q4":"8350.0000","q2":"8350.0000","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"0.0000","q193":"1.0000","q70":"0.0000","q63":"8350.0000","q59":"2022-10-17 00:00:00"}},{"info":[{"p1688":{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}}],"quote":{"q68":"PPlibaojia","q124":"JO_112033","q67":"PP粒报价","q1":"8391.6700","q3":"8391.6700","q4":"8391.6700","q2":"8391.6700","q60":"1.0000","q62":"0.0000","qhistoryStyle":"3.0000","q128":"0.0000","q80":"-0.4940","q193":"1.0000","q70":"-41.6602","q63":"8391.6700","q59":"2022-10-14 00:00:00"}}]}]
```
是的大佬,就是终端的问题。
刚开始用的go的是一个三方库(requests),然后发现这个问题,一看那个库好几年没更新了想是不是库的问题,然后转标准库尝试还是这样。然后又考虑是不是响应编码解码的问题,因为之前py经常遇到编码的问题,但后来转念一想编码出错一般情况下不会只错其中几个字符。
go用的是goland,python用的是vscode,把go代码放到vscode里运行后结果和python的一样了。
仔细检查了一下发现goland终端打印的结果里 `{"name":"珠宝品牌","unit":"元/吨","value":"塑料行情"}` 这一段第一个逗号那里里出现了 *字符重合* 的问题。
goland就安装激活然后安装了个中文插件还安装了个主题,别的就啥也没动,不明白问啥出这样的问题。
#4
更多评论