fuhefei_edb.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 FuHeFeiListMap = map[string]string {
  11. "中国磷铵产能利用率周数据统计" : "https://list.oilchem.net/192/6522/",
  12. "中国钾肥样本港口库存量周数据统计" : "https://list.oilchem.net/192/50857/",
  13. "中国复合肥进出口月数据分析" : "https://list.oilchem.net/192/5379/",
  14. }
  15. func FuHeFeiList(num int) (err error) {
  16. for k, v := range FuHeFeiListMap {
  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 AnalysisOilchemFuHeFei1(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: 3,
  125. Unit: unit,
  126. Frequency: "周度",
  127. Describe: "",
  128. DataTime: dataTime,
  129. Value: value,
  130. Sort: 0,
  131. CreateTime: time.Now(),
  132. ModifyTime: time.Now(),
  133. IndexNameStr: "中国磷铵产能利用率",
  134. MarketName: area,
  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 AnalysisOilchemFuHeFei2(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: 3,
  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. }
  262. // 中国复合肥进出口月数据分析
  263. func AnalysisOilchemFuHeFei3(htm []byte) (err error) {
  264. if len(htm) == 0 {
  265. utils.FileLog.Info("htm empty")
  266. return
  267. }
  268. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  269. if e != nil {
  270. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  271. return
  272. }
  273. titleList := make([]string, 0)
  274. unitList := make([]string, 0)
  275. doc.Find("p[style*='text-align: center;']").Each(func(i int, selection *goquery.Selection) {
  276. ptext := selection.Text()
  277. if strings.Contains(ptext, "单位:") {
  278. unit := strings.Replace(ptext, "单位:", "", -1)
  279. //fmt.Println("unit:",unit)
  280. unitList = append(unitList, unit)
  281. }
  282. if strings.Contains(ptext, "统计") {
  283. title := ptext
  284. //fmt.Println("title:",title)
  285. titleList = append(titleList, title)
  286. }
  287. })
  288. area := ""
  289. title := doc.Find("h2").Text()
  290. fmt.Println("title:",title)
  291. createTimeStr := doc.Find("h2").Next().Text()
  292. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  293. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  294. createTimeStr = createTimeStr[:createTimeStrIndex]
  295. createTimeStr = strings.TrimSpace(createTimeStr)
  296. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  297. if err != nil {
  298. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  299. return
  300. }
  301. //fmt.Println("createTime:",createTime)
  302. dataTime := createTime.Format(utils.FormatDate)
  303. //fmt.Println("dataTime:",dataTime)
  304. indexList := make([]*models.BaseFromOilchemIndex, 0)
  305. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  306. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  307. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  308. text3 := table3.Text()
  309. text3 = strings.Replace(text3,"\n","",-1)
  310. text3 = strings.Replace(text3," ","",-1)
  311. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  312. return
  313. }
  314. //fmt.Println("table3:",text3)
  315. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  316. //fmt.Println("ii:",ii)
  317. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  318. //fmt.Println("jj:",jj)
  319. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  320. //fmt.Println("tableIndex:",tableIndex)
  321. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  322. if utils.ContainsChinese(text3) && text3 != "本周" {
  323. area = text3
  324. return
  325. }
  326. if area == "" {
  327. return
  328. }
  329. value := text3
  330. value = strings.TrimRight(value, "%")
  331. //valueF, e := strconv.ParseFloat(value, 64)
  332. //if e != nil {
  333. // err = e
  334. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  335. // return
  336. //}
  337. title := titleList[tableIndex]
  338. //fmt.Println("tbody title:",title)
  339. if strings.Contains(title,"中国氮磷钾复合肥按产销国进口量月数据统计") {
  340. title = "中国氮磷钾复合肥产销国进口量"
  341. } else if strings.Contains(title,"中国氮磷钾复合肥按贸易方式进口量月数据统计") {
  342. title = "中国氮磷钾复合肥贸易方式进口量"
  343. } else if strings.Contains(title,"中国氮磷钾三元肥产销国出口量月数据统计") {
  344. title = "中国氮磷钾复合肥产销国出口量"
  345. } else if strings.Contains(title,"中国氮磷钾三元肥贸易方式出口量月数据统计") {
  346. title = "中国氮磷钾复合肥贸易方式出口量"
  347. } else {
  348. return
  349. }
  350. indexName := title + "(" +area+")"
  351. fmt.Println("indexName:",indexName)
  352. fmt.Println("valueF:",value)
  353. //unit := ""
  354. //if strings.Contains(area,"产量") {
  355. // unit = "万吨"
  356. //} else {
  357. // unit = "%"
  358. //}
  359. item := &models.BaseFromOilchemIndex{
  360. IndexName: indexName,
  361. ClassifyId: 3,
  362. Unit: "吨",
  363. Frequency: "月度",
  364. Describe: "",
  365. DataTime: dataTime,
  366. Value: value,
  367. Sort: 0,
  368. CreateTime: time.Now(),
  369. ModifyTime: time.Now(),
  370. IndexNameStr: title,
  371. MarketName: area,
  372. }
  373. indexList = append(indexList,item)
  374. area = ""
  375. })
  376. })
  377. })
  378. //写入数据库
  379. err = PostHandleOilchem(indexList)
  380. if err != nil {
  381. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  382. fmt.Println("PostHandleOilchem err",err)
  383. return
  384. }
  385. return
  386. }