zhijiang_edb.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package services
  2. import (
  3. "eta/eta_data_analysis/models"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "github.com/PuerkitoBio/goquery"
  7. "strings"
  8. "time"
  9. )
  10. var ZhiJiangListMap = map[string]string {
  11. "中国化机浆样本产量周数据分析" : "https://list.oilchem.net/2959/45240/",
  12. "中国阔叶浆样本产量周数据分析" : "https://list.oilchem.net/2959/45240/",
  13. "中国纸浆主流港口样本库存周数据分析" : "https://list.oilchem.net/2959/45241/",
  14. }
  15. func ZhiJiangList(num int) (err error) {
  16. for k, v := range ZhiJiangListMap {
  17. for i := 1; i < num; i++ {
  18. listUrl := v + fmt.Sprintf("%d.html",i)
  19. fmt.Println("listUrl:",listUrl)
  20. htm, e := FetchPageHtml(listUrl)
  21. if e != nil {
  22. err = e
  23. utils.FileLog.Error(fmt.Sprintf("FetchPageHtml err:%v",err))
  24. fmt.Println("FetchPageHtml err",err)
  25. return
  26. }
  27. err = AnalysisOilchemList(htm, k)
  28. if err != nil {
  29. utils.FileLog.Error(fmt.Sprintf("AnalysisOilchemList err:%v",err))
  30. fmt.Println("AnalysisOilchemList err",err)
  31. return
  32. }
  33. time.Sleep(2*time.Second)
  34. }
  35. }
  36. return
  37. }
  38. // 中国化机浆样本产量,中国阔叶浆样本产量
  39. func AnalysisOilchemZhiJiang1(htm []byte) (err error) {
  40. if len(htm) == 0 {
  41. utils.FileLog.Info("htm empty")
  42. return
  43. }
  44. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  45. if e != nil {
  46. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  47. return
  48. }
  49. //titleList := make([]string, 0)
  50. unitList := make([]string, 0)
  51. doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  52. ptext := selection.Text()
  53. if strings.Contains(ptext, "单位:") {
  54. unit := strings.Replace(ptext, "单位:", "", -1)
  55. //fmt.Println("unit:",unit)
  56. unitList = append(unitList, unit)
  57. }
  58. //if strings.Contains(ptext, "中国纸浆主流港口样本库存周数据") {
  59. // title := ptext
  60. // //fmt.Println("title:",title)
  61. // titleList = append(titleList, title)
  62. //}
  63. })
  64. area := ""
  65. title := doc.Find("h2").Text()
  66. fmt.Println("title:",title)
  67. createTimeStr := doc.Find("h2").Next().Text()
  68. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  69. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  70. createTimeStr = createTimeStr[:createTimeStrIndex]
  71. createTimeStr = strings.TrimSpace(createTimeStr)
  72. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  73. if err != nil {
  74. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  75. return
  76. }
  77. //fmt.Println("createTime:",createTime)
  78. dataTime := createTime.Format(utils.FormatDate)
  79. //fmt.Println("dataTime:",dataTime)
  80. indexList := make([]*models.BaseFromOilchemIndex, 0)
  81. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  82. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  83. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  84. text3 := table3.Text()
  85. text3 = strings.Replace(text3,"\n","",-1)
  86. text3 = strings.Replace(text3," ","",-1)
  87. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  88. return
  89. }
  90. //fmt.Println("table3:",text3)
  91. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  92. //fmt.Println("ii:",ii)
  93. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  94. //fmt.Println("jj:",jj)
  95. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  96. //fmt.Println("tableIndex:",tableIndex)
  97. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  98. if utils.ContainsChinese(text3) && text3 != "本周" {
  99. area = text3
  100. return
  101. }
  102. if area == "" {
  103. return
  104. }
  105. value := text3
  106. value = strings.TrimRight(value, "%")
  107. //valueF, e := strconv.ParseFloat(value, 64)
  108. //if e != nil {
  109. // err = e
  110. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  111. // return
  112. //}
  113. indexName := "中国" + area + "样本产量"
  114. fmt.Println("indexName:",indexName)
  115. fmt.Println("valueF:",value)
  116. //unit := ""
  117. //if strings.Contains(area,"产量") {
  118. // unit = "万吨"
  119. //} else {
  120. // unit = "%"
  121. //}
  122. item := &models.BaseFromOilchemIndex{
  123. IndexName: indexName,
  124. ClassifyId: 4,
  125. Unit: "万吨",
  126. Frequency: "周度",
  127. Describe: "",
  128. DataTime: dataTime,
  129. Value: value,
  130. Sort: 0,
  131. CreateTime: time.Now(),
  132. ModifyTime: time.Now(),
  133. IndexNameStr: indexName,
  134. MarketName: "",
  135. }
  136. indexList = append(indexList,item)
  137. area = ""
  138. })
  139. })
  140. })
  141. // 写入数据库
  142. err = PostHandleOilchem(indexList)
  143. if err != nil {
  144. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  145. fmt.Println("PostHandleOilchem err",err)
  146. return
  147. }
  148. return
  149. }
  150. // 中国纸浆主流港口样本库存周数据
  151. func AnalysisOilchemZhiJiang2(htm []byte) (err error) {
  152. if len(htm) == 0 {
  153. utils.FileLog.Info("htm empty")
  154. return
  155. }
  156. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  157. if e != nil {
  158. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  159. return
  160. }
  161. //titleList := make([]string, 0)
  162. unitList := make([]string, 0)
  163. doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  164. ptext := selection.Text()
  165. if strings.Contains(ptext, "单位:") {
  166. unit := strings.Replace(ptext, "单位:", "", -1)
  167. //fmt.Println("unit:",unit)
  168. unitList = append(unitList, unit)
  169. }
  170. //if strings.Contains(ptext, "中国纸浆主流港口样本库存周数据") {
  171. // title := ptext
  172. // //fmt.Println("title:",title)
  173. // titleList = append(titleList, title)
  174. //}
  175. })
  176. area := ""
  177. title := doc.Find("h2").Text()
  178. fmt.Println("title:",title)
  179. createTimeStr := doc.Find("h2").Next().Text()
  180. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  181. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  182. createTimeStr = createTimeStr[:createTimeStrIndex]
  183. createTimeStr = strings.TrimSpace(createTimeStr)
  184. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  185. if err != nil {
  186. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  187. return
  188. }
  189. //fmt.Println("createTime:",createTime)
  190. dataTime := createTime.Format(utils.FormatDate)
  191. //fmt.Println("dataTime:",dataTime)
  192. indexList := make([]*models.BaseFromOilchemIndex, 0)
  193. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  194. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  195. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  196. text3 := table3.Text()
  197. text3 = strings.Replace(text3,"\n","",-1)
  198. text3 = strings.Replace(text3," ","",-1)
  199. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  200. return
  201. }
  202. //fmt.Println("table3:",text3)
  203. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  204. //fmt.Println("ii:",ii)
  205. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  206. //fmt.Println("jj:",jj)
  207. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  208. //fmt.Println("tableIndex:",tableIndex)
  209. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  210. if utils.ContainsChinese(text3) && text3 != "本周" {
  211. area = text3
  212. return
  213. }
  214. if area == "" {
  215. return
  216. }
  217. value := text3
  218. value = strings.TrimRight(value, "%")
  219. //valueF, e := strconv.ParseFloat(value, 64)
  220. //if e != nil {
  221. // err = e
  222. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  223. // return
  224. //}
  225. indexName := "中国纸浆主流港口样本库存" +"("+ area + ")"
  226. fmt.Println("indexName:",indexName)
  227. fmt.Println("valueF:",value)
  228. //unit := ""
  229. //if strings.Contains(area,"产量") {
  230. // unit = "万吨"
  231. //} else {
  232. // unit = "%"
  233. //}
  234. item := &models.BaseFromOilchemIndex{
  235. IndexName: indexName,
  236. ClassifyId: 4,
  237. Unit: "万吨",
  238. Frequency: "周度",
  239. Describe: "",
  240. DataTime: dataTime,
  241. Value: value,
  242. Sort: 0,
  243. CreateTime: time.Now(),
  244. ModifyTime: time.Now(),
  245. IndexNameStr: "中国纸浆主流港口样本库存",
  246. MarketName: area,
  247. }
  248. indexList = append(indexList,item)
  249. area = ""
  250. })
  251. })
  252. })
  253. // 写入数据库
  254. err = PostHandleOilchem(indexList)
  255. if err != nil {
  256. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  257. fmt.Println("PostHandleOilchem err",err)
  258. return
  259. }
  260. return
  261. }