数字输出总是为0
来源:15-9 用户信息解析器(下)

qq_慕丝7361439
2020-04-14
测试部分
contents,err := ioutil.ReadFile(“profile_test_data.html”)
if err != nil{
panic(err)
}
result := ParseProfile(contents,"","执手不离小短腿i")
if len(result.Items) != 1{
t.Errorf("result should contain 1 element: but was %v",result.Items)
}
actual := result.Items[0]
expected :=engine.Item{
Url: "",
Type: "zhenai",
Id:"",
Payload: model.Profile{
Name: "执手不离小短腿i",
Gender: "男",
Age: 94,
Height: 29,
Weight: 263 ,
Income: "2001-3000元",
Xinzuo: "天蝎座",
Occupation: "销售",
Marriage: "未婚",
House: "租房",
Hokou: "宁波市",
Education: "大学",
Car: "无车",
},
}
profile部分
var ageRe = regexp.MustCompile(`<td><span class="label">年龄:</span>(\d+)岁</td>`)
var heightRe = regexp.MustCompile(`<td><span class="label">身高:</span>(\d+)CM</td>`)
var marriageRe = regexp.MustCompile(`<td><span class="label">婚况:</span>([^<]+)</td>`)
profile := model.Profile{}
profile.Name = name
age,err := strconv.Atoi(extractString(contents,ageRe))
if err!=nil{
profile.Age = age
}
height,err := strconv.Atoi(extractString(contents,heightRe))
if err!=nil{
profile.Height = height
}
weight,err := strconv.Atoi(extractString(contents,weightRe))
if err!=nil{
profile.Weight = weight
}
result := engine.ParseResult{
Items: []engine.Item{
{
Url: url ,
Type: "zhenai",
Id: extractString([]byte(url), idUrlRe),
Payload: profile,
},
},
}
matches := guessRe.FindAllSubmatch(contents, -1)
for _,m := range matches{
result.Requests = append(result.Requests,engine.Request{
Url: string(m[1]),
ParserFunc: ProfileParser(string(m[2])),
})
}
return result
然后输出的身高体重就是0
写回答
2回答
-
国服第一PHP
2020-10-18
这个是获取到age height weight 下面判断err != nil这边写错了,老师一开始也写错了,但是后面改了,这边视频省略掉了,应该是err == nil,大家可以注意一下
00 -
ccmouse
2020-04-17
我们的测试跑出来对吗?如果都是0的话,测试应该也会挂
052020-08-23
相似问题