@@ -102,25 +102,28 @@ func trimTitleTags(title string) string {
102102
103103//获取title
104104func GetTitle (r * xhttp.Response ) (title string ) {
105- println ( r .GetStatus ())
106- body := string (r .GetBody ())
107- var re = regexp .MustCompile (`(?im)<\s*title.*>(.*?)<\s*/\s*title>` )
108- for _ , match := range re .FindAllString (body , - 1 ) {
109- title = html .UnescapeString (trimTitleTags (match ))
110- break
111- }
112- // Non UTF-8
113- if contentTypes , ok := r .GetHeaders ()["Content-Type" ]; ok {
114- contentType := strings .ToLower (strings .Join (contentTypes , ";" ))
115- // special cases
116- if strings .Contains (contentType , "charset=gb2312" ) || strings .Contains (contentType , "charset=gbk" ) {
117- titleUtf8 , err := Decodegbk ([]byte (title ))
118- if err != nil {
119- return
120- }
105+ if r .GetStatus () == 200 {
106+ body := string (r .GetBody ())
107+ var re = regexp .MustCompile (`(?im)<\s*title.*>(.*?)<\s*/\s*title>` )
108+ for _ , match := range re .FindAllString (body , - 1 ) {
109+ title = html .UnescapeString (trimTitleTags (match ))
110+ break
111+ }
112+ // Non UTF-8
113+ if contentTypes , ok := r .GetHeaders ()["Content-Type" ]; ok {
114+ contentType := strings .ToLower (strings .Join (contentTypes , ";" ))
115+ // special cases
116+ if strings .Contains (contentType , "charset=gb2312" ) || strings .Contains (contentType , "charset=gbk" ) {
117+ titleUtf8 , err := Decodegbk ([]byte (title ))
118+ if err != nil {
119+ return
120+ }
121121
122- return string (titleUtf8 )
122+ return string (titleUtf8 )
123+ }
123124 }
125+ return
124126 }
125- return
127+ return ""
128+
126129}
0 commit comments