jiachun_edb.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "github.com/PuerkitoBio/goquery"
  8. "strings"
  9. "time"
  10. )
  11. // OilchemTaskAnalysisHandlers 解析表格的函数
  12. var OilchemTaskAnalysisHandlers = map[string]func(htm []byte) (err error){
  13. "[产量/产能利用率]:中国甲醇产量及产能利用率周数据统计": AnalysisOilchemJiaChun1,
  14. "库存周数据分析": AnalysisOilchemJiaChun2,
  15. "中国甲醇样本生产企业库存及订单待发周数据统计": AnalysisOilchemJiaChun3,
  16. "中国甲醇样本利润周数据统计": AnalysisOilchemJiaChun4,
  17. "中国甲醇产量及产能利用率": AnalysisOilchemJiaChun5,
  18. "中国甲醇下游样本产能利用率周数据": AnalysisOilchemJiaChun6,
  19. "中国甲醇样本生产企业库存及订单待发周数据分析": AnalysisOilchemJiaChun7,
  20. "中国尿素分原料产能利用率周数据统计": AnalysisOilchemNiaoSu1,
  21. "中国尿素样本生产理论利润周数据统计": AnalysisOilchemNiaoSu2,
  22. "中国尿素样本港口库存周数据统计": AnalysisOilchemNiaoSu3,
  23. "中国尿素产量周数据分析": AnalysisOilchemNiaoSu4,
  24. "中国尿素企业库存周数据分析": AnalysisOilchemNiaoSu5,
  25. "中国尿素样本港口库存周数据分析": AnalysisOilchemNiaoSu6,
  26. "中国尿素企业预收订单周数据分析": AnalysisOilchemNiaoSu7,
  27. "中国尿素样本理论利润周数据分析": AnalysisOilchemNiaoSu8,
  28. "中国复合肥产能利用率周数据统计": AnalysisOilchemNiaoSu9,
  29. "中国三聚氰胺产能利用率周数据统计": AnalysisOilchemNiaoSu10,
  30. "中国尿素产量分省份月数据统计": AnalysisOilchemNiaoSu11,
  31. "中国尿素进出口量数据总体分析": AnalysisOilchemNiaoSu12,
  32. "中国磷铵产能利用率周数据统计": AnalysisOilchemFuHeFei1,
  33. "中国钾肥样本港口库存量周数据统计": AnalysisOilchemFuHeFei2,
  34. "中国复合肥进出口月数据分析": AnalysisOilchemFuHeFei3,
  35. "中国阔叶浆样本产量周数据分析": AnalysisOilchemZhiJiang1,
  36. "中国化机浆样本产量周数据分析": AnalysisOilchemZhiJiang1,
  37. "中国纸浆主流港口样本库存周数据分析": AnalysisOilchemZhiJiang2,
  38. "国内独立炼厂产能利用率周数据统计": AnalysisOilchemYuanYou1,
  39. "国内炼厂常减压装置产能利用率月数据统计": AnalysisOilchemYuanYou2,
  40. "山东独立炼厂原油到港量周数据统计": AnalysisOilchemYuanYou3,
  41. "中国港口商业原油库存指数分析": AnalysisOilchemYuanYou4,
  42. "山东独立炼厂原油样本库容率周数据分析": AnalysisOilchemYuanYou5,
  43. "中国炼厂原油加工量月数据分析": AnalysisOilchemYuanYou6,
  44. "国内原油加工量简况": AnalysisOilchemYuanYou7,
  45. "国内原油产量表": AnalysisOilchemYuanYou8,
  46. "中国原油进出口量月数据统计": AnalysisOilchemYuanYou9,
  47. "中国原油月度进出口数据分析报告": AnalysisOilchemYuanYou10,
  48. "美国能源信息署最新石油库存报告": AnalysisOilchemYuanYou11,
  49. "美国API库存数据": AnalysisOilchemYuanYou12,
  50. "国际主要汇率收盘": AnalysisOilchemYuanYou13,
  51. }
  52. var JiaChunListMap = map[string]string {
  53. "[产量/产能利用率]:中国甲醇产量及产能利用率周数据统计" : "https://list.oilchem.net/140/38266/",
  54. "库存周数据分析" : "https://list.oilchem.net/140/38271/",
  55. "中国甲醇样本生产企业库存及订单待发周数据统计" : "https://list.oilchem.net/140/1086/",
  56. "中国甲醇样本利润周数据统计" : "https://list.oilchem.net/140/1088/",
  57. "中国甲醇产量及产能利用率" : "https://list.oilchem.net/140/1087/",
  58. "中国甲醇下游样本产能利用率周数据" : "https://list.oilchem.net/140/38270/",
  59. "中国甲醇样本生产企业库存及订单待发周数据分析" : "https://list.oilchem.net/140/38271/",
  60. }
  61. // 中国甲醇产量及产能利用率周数据统计
  62. func AnalysisOilchemJiaChun1(htm []byte) (err error) {
  63. if len(htm) == 0 {
  64. utils.FileLog.Info("htm empty")
  65. return
  66. }
  67. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  68. if e != nil {
  69. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  70. return
  71. }
  72. suffixList := make([]string, 0)
  73. unitList := make([]string, 0)
  74. doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  75. ptext := selection.Text()
  76. if strings.Contains(ptext, "单位:") {
  77. unit := strings.Replace(ptext, "单位:", "", -1)
  78. //fmt.Println("unit:",unit)
  79. unitList = append(unitList, unit)
  80. }
  81. if strings.Contains(ptext, "中国甲醇产量分地区") {
  82. suffix := ptext
  83. //fmt.Println("title:",title)
  84. if strings.Contains(ptext, "利用率") {
  85. suffix = "产能利用率"
  86. } else {
  87. suffix = "产量"
  88. }
  89. suffixList = append(suffixList, suffix)
  90. }
  91. })
  92. area := ""
  93. title := doc.Find("h2").Text()
  94. fmt.Println("title:",title)
  95. createTimeStr := doc.Find("h2").Next().Text()
  96. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  97. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  98. createTimeStr = createTimeStr[:createTimeStrIndex]
  99. createTimeStr = strings.TrimSpace(createTimeStr)
  100. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  101. if err != nil {
  102. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  103. return
  104. }
  105. //fmt.Println("createTime:",createTime)
  106. dataTime := createTime.Format(utils.FormatDate)
  107. //fmt.Println("dataTime:",dataTime)
  108. indexList := make([]*models.BaseFromOilchemIndex, 0)
  109. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  110. mid := "分地区"
  111. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  112. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  113. text3 := table3.Text()
  114. text3 = strings.Replace(text3,"\n","",-1)
  115. text3 = strings.Replace(text3," ","",-1)
  116. text3 = strings.Replace(text3,"\u00a0","",-1)
  117. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  118. return
  119. }
  120. //fmt.Println("table3:",text3)
  121. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  122. //fmt.Println("ii:",ii)
  123. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  124. //fmt.Println("jj:",jj)
  125. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  126. //fmt.Println("tableIndex:",tableIndex)
  127. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  128. if utils.ContainsChinese(text3) && text3 != "本周" {
  129. area = text3
  130. return
  131. }
  132. if area == "" {
  133. return
  134. }
  135. if area == "工艺类型" {
  136. mid = "分工艺"
  137. return
  138. }
  139. title = "中国甲醇产量" + mid + suffixList[tableIndex]
  140. value := text3
  141. value = strings.TrimRight(value, "%")
  142. //valueF, e := strconv.ParseFloat(value, 64)
  143. //if e != nil {
  144. // err = e
  145. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  146. // return
  147. //}
  148. indexName := title + "(" + area +")"
  149. fmt.Println("indexName:",indexName)
  150. fmt.Println("valueF:",value)
  151. unit := unitList[tableIndex]
  152. item := &models.BaseFromOilchemIndex{
  153. IndexName: indexName,
  154. ClassifyId: 1,
  155. Unit: unit,
  156. Frequency: "周度",
  157. Describe: "",
  158. DataTime: dataTime,
  159. Value: value,
  160. Sort: 0,
  161. CreateTime: time.Now(),
  162. ModifyTime: time.Now(),
  163. IndexNameStr: title,
  164. MarketName: area,
  165. }
  166. indexList = append(indexList,item)
  167. area = ""
  168. })
  169. })
  170. })
  171. // 写入数据库
  172. err = PostHandleOilchem(indexList)
  173. if err != nil {
  174. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  175. fmt.Println("PostHandleOilchem err",err)
  176. return
  177. }
  178. return
  179. }
  180. // 中国甲醇样本港口库存周数据统计
  181. func AnalysisOilchemJiaChun2(htm []byte) (err error) {
  182. if len(htm) == 0 {
  183. utils.FileLog.Info("htm empty")
  184. return
  185. }
  186. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  187. if e != nil {
  188. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  189. return
  190. }
  191. titleList := make([]string, 0)
  192. unitList := make([]string, 0)
  193. doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  194. ptext := selection.Text()
  195. if strings.Contains(ptext, "趋势") {
  196. return
  197. }
  198. if strings.Contains(ptext, "单位:") {
  199. unit := strings.Replace(ptext, "单位:", "", -1)
  200. //fmt.Println("unit:",unit)
  201. unitList = append(unitList, unit)
  202. }
  203. if strings.Contains(ptext, "中国甲醇港口样本库存周数据对比") {
  204. title := "中国甲醇港口库存"
  205. //fmt.Println("title:",title)
  206. titleList = append(titleList, title)
  207. }
  208. })
  209. area := ""
  210. title := doc.Find("h2").Text()
  211. fmt.Println("title:",title)
  212. createTimeStr := doc.Find("h2").Next().Text()
  213. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  214. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  215. createTimeStr = createTimeStr[:createTimeStrIndex]
  216. createTimeStr = strings.TrimSpace(createTimeStr)
  217. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  218. if err != nil {
  219. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  220. return
  221. }
  222. //fmt.Println("createTime:",createTime)
  223. dataTime := createTime.Format(utils.FormatDate)
  224. fmt.Println("dataTime:",dataTime)
  225. indexList := make([]*models.BaseFromOilchemIndex, 0)
  226. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  227. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  228. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  229. text3 := table3.Text()
  230. text3 = strings.Replace(text3,"\n","",-1)
  231. text3 = strings.Replace(text3," ","",-1)
  232. text3 = strings.Replace(text3,"\u00a0","",-1)
  233. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  234. return
  235. }
  236. //fmt.Println("table3:",text3)
  237. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  238. //fmt.Println("ii:",ii)
  239. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  240. //fmt.Println("jj:",jj)
  241. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  242. //fmt.Println("tableIndex:",tableIndex)
  243. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  244. if utils.ContainsChinese(text3) && text3 != "本周" {
  245. area = text3
  246. return
  247. }
  248. if area == "" {
  249. return
  250. }
  251. value := text3
  252. value = strings.TrimRight(value, "%")
  253. //valueF, e := strconv.ParseFloat(value, 64)
  254. //if e != nil {
  255. // err = e
  256. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  257. // return
  258. //}
  259. title := "中国甲醇港口库存"
  260. indexName := title + "(" + area +")"
  261. unit := "万吨"
  262. fmt.Println("indexName:",indexName)
  263. fmt.Println("valueF:",value)
  264. fmt.Println("unit:",unit)
  265. item := &models.BaseFromOilchemIndex{
  266. IndexName: indexName,
  267. ClassifyId: 1,
  268. Unit: unit,
  269. Frequency: "周度",
  270. Describe: "",
  271. DataTime: dataTime,
  272. Value: value,
  273. Sort: 0,
  274. CreateTime: time.Now(),
  275. ModifyTime: time.Now(),
  276. IndexNameStr: title,
  277. MarketName: area,
  278. }
  279. indexList = append(indexList,item)
  280. area = ""
  281. })
  282. })
  283. })
  284. // 写入数据库
  285. err = PostHandleOilchem(indexList)
  286. if err != nil {
  287. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  288. fmt.Println("PostHandleOilchem err",err)
  289. return
  290. }
  291. return
  292. }
  293. // 中国甲醇样本生产企业库存及订单待发周数据统计
  294. func AnalysisOilchemJiaChun3(htm []byte) (err error) {
  295. if len(htm) == 0 {
  296. utils.FileLog.Info("htm empty")
  297. return
  298. }
  299. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  300. if e != nil {
  301. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  302. return
  303. }
  304. titleList := make([]string, 0)
  305. unitList := make([]string, 0)
  306. doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  307. ptext := selection.Text()
  308. if strings.Contains(ptext, "趋势") {
  309. return
  310. }
  311. if strings.Contains(ptext, "单位:") {
  312. unit := strings.Replace(ptext, "单位:", "", -1)
  313. //fmt.Println("unit:",unit)
  314. unitList = append(unitList, unit)
  315. }
  316. //https://www.oilchem.net/22-1026-13-e6e4dd2d1d3d9309.html 10-26的表格没有名字。。。
  317. //if strings.Contains(ptext, "中国甲醇样本生产企业") {
  318. // title := ptext
  319. // //fmt.Println("title:",title)
  320. // if strings.Contains(title, "订单待发") {
  321. // title = "中国甲醇样本生产企业订单待发"
  322. // } else {
  323. // title = "中国甲醇样本生产企业库存"
  324. // }
  325. // titleList = append(titleList, title)
  326. //}
  327. titleList = append(titleList, "中国甲醇样本生产企业库存","中国甲醇样本生产企业订单待发")
  328. })
  329. area := ""
  330. title := doc.Find("h2").Text()
  331. fmt.Println("title:",title)
  332. createTimeStr := doc.Find("h2").Next().Text()
  333. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  334. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  335. createTimeStr = createTimeStr[:createTimeStrIndex]
  336. createTimeStr = strings.TrimSpace(createTimeStr)
  337. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  338. if err != nil {
  339. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  340. return
  341. }
  342. //fmt.Println("createTime:",createTime)
  343. dataTime := createTime.Format(utils.FormatDate)
  344. fmt.Println("dataTime:",dataTime)
  345. indexList := make([]*models.BaseFromOilchemIndex, 0)
  346. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  347. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  348. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  349. text3 := table3.Text()
  350. text3 = strings.Replace(text3,"\n","",-1)
  351. text3 = strings.Replace(text3," ","",-1)
  352. text3 = strings.Replace(text3,"\u00a0","",-1)
  353. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  354. return
  355. }
  356. //fmt.Println("table3:",text3)
  357. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  358. //fmt.Println("ii:",ii)
  359. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  360. //fmt.Println("jj:",jj)
  361. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  362. //fmt.Println("tableIndex:",tableIndex)
  363. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  364. if utils.ContainsChinese(text3) && text3 != "本周" {
  365. area = text3
  366. return
  367. }
  368. if area == "" {
  369. return
  370. }
  371. value := text3
  372. value = strings.TrimRight(value, "%")
  373. //valueF, e := strconv.ParseFloat(value, 64)
  374. //if e != nil {
  375. // err = e
  376. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  377. // return
  378. //}
  379. indexName := titleList[tableIndex] + "(" + area +")"
  380. unit := "万吨"
  381. fmt.Println("indexName:",indexName)
  382. //fmt.Println("valueF:",valueF)
  383. fmt.Println("unit:",unit)
  384. item := &models.BaseFromOilchemIndex{
  385. IndexName: indexName,
  386. ClassifyId: 1,
  387. Unit: unit,
  388. Frequency: "周度",
  389. Describe: "",
  390. DataTime: dataTime,
  391. Value: value,
  392. Sort: 0,
  393. CreateTime: time.Now(),
  394. ModifyTime: time.Now(),
  395. IndexNameStr: titleList[tableIndex],
  396. MarketName: area,
  397. }
  398. indexList = append(indexList,item)
  399. area = ""
  400. })
  401. })
  402. })
  403. // 写入数据库
  404. err = PostHandleOilchem(indexList)
  405. if err != nil {
  406. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  407. fmt.Println("PostHandleOilchem err",err)
  408. return
  409. }
  410. return
  411. }
  412. // 中国甲醇样本利润周数据统计
  413. func AnalysisOilchemJiaChun4(htm []byte) (err error) {
  414. if len(htm) == 0 {
  415. utils.FileLog.Info("htm empty")
  416. return
  417. }
  418. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  419. if e != nil {
  420. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  421. return
  422. }
  423. //titleList := make([]string, 0)
  424. //unitList := make([]string, 0)
  425. //doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  426. // ptext := selection.Text()
  427. // if strings.Contains(ptext, "趋势") {
  428. // return
  429. // }
  430. // if strings.Contains(ptext, "单位:") {
  431. // unit := strings.Replace(ptext, "单位:", "", -1)
  432. // //fmt.Println("unit:",unit)
  433. // unitList = append(unitList, unit)
  434. // }
  435. // if strings.Contains(ptext, "中国甲醇样本利润") {
  436. // title := ptext
  437. // //fmt.Println("title:",title)
  438. // titleList = append(titleList, title)
  439. // }
  440. //})
  441. area := ""
  442. title := doc.Find("h2").Text()
  443. fmt.Println("title:",title)
  444. createTimeStr := doc.Find("h2").Next().Text()
  445. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  446. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  447. createTimeStr = createTimeStr[:createTimeStrIndex]
  448. createTimeStr = strings.TrimSpace(createTimeStr)
  449. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  450. if err != nil {
  451. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  452. return
  453. }
  454. //fmt.Println("createTime:",createTime)
  455. dataTime := createTime.Format(utils.FormatDate)
  456. fmt.Println("dataTime:",dataTime)
  457. indexList := make([]*models.BaseFromOilchemIndex, 0)
  458. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  459. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  460. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  461. text3 := table3.Text()
  462. text3 = strings.Replace(text3,"\n","",-1)
  463. text3 = strings.Replace(text3," ","",-1)
  464. text3 = strings.Replace(text3,"\u00a0","",-1)
  465. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  466. return
  467. }
  468. //fmt.Println("table3:",text3)
  469. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  470. //fmt.Println("ii:",ii)
  471. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  472. //fmt.Println("jj:",jj)
  473. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  474. //fmt.Println("tableIndex:",tableIndex)
  475. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  476. if utils.ContainsChinese(text3) && text3 != "本周" {
  477. area = text3
  478. return
  479. }
  480. if area == "" {
  481. return
  482. }
  483. value := text3
  484. value = strings.TrimRight(value, "%")
  485. //valueF, e := strconv.ParseFloat(value, 64)
  486. if e != nil {
  487. err = e
  488. utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  489. return
  490. }
  491. indexName := area
  492. unit := "元/吨"
  493. fmt.Println("indexName:",indexName)
  494. //fmt.Println("valueF:",valueF)
  495. fmt.Println("unit:",unit)
  496. item := &models.BaseFromOilchemIndex{
  497. IndexName: indexName,
  498. ClassifyId: 1,
  499. Unit: unit,
  500. Frequency: "周度",
  501. Describe: "",
  502. DataTime: dataTime,
  503. Value: value,
  504. Sort: 0,
  505. CreateTime: time.Now(),
  506. ModifyTime: time.Now(),
  507. IndexNameStr: indexName,
  508. MarketName: "",
  509. }
  510. indexList = append(indexList,item)
  511. area = ""
  512. })
  513. })
  514. })
  515. // 写入数据库
  516. err = PostHandleOilchem(indexList)
  517. if err != nil {
  518. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  519. fmt.Println("PostHandleOilchem err",err)
  520. return
  521. }
  522. return
  523. }
  524. // 中国甲醇产量及产能利用率周数据
  525. func AnalysisOilchemJiaChun5(htm []byte) (err error) {
  526. if len(htm) == 0 {
  527. utils.FileLog.Info("htm empty")
  528. return
  529. }
  530. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  531. if e != nil {
  532. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  533. return
  534. }
  535. //titleList := make([]string, 0)
  536. //unitList := make([]string, 0)
  537. //doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  538. // ptext := selection.Text()
  539. // if strings.Contains(ptext, "趋势") {
  540. // return
  541. // }
  542. // if strings.Contains(ptext, "单位:") {
  543. // unit := strings.Replace(ptext, "单位:", "", -1)
  544. // //fmt.Println("unit:",unit)
  545. // unitList = append(unitList, unit)
  546. // }
  547. // if strings.Contains(ptext, "中国甲醇样本利润") {
  548. // title := ptext
  549. // //fmt.Println("title:",title)
  550. // titleList = append(titleList, title)
  551. // }
  552. //})
  553. area := ""
  554. title := doc.Find("h2").Text()
  555. fmt.Println("title:",title)
  556. createTimeStr := doc.Find("h2").Next().Text()
  557. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  558. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  559. createTimeStr = createTimeStr[:createTimeStrIndex]
  560. createTimeStr = strings.TrimSpace(createTimeStr)
  561. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  562. if err != nil {
  563. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  564. return
  565. }
  566. //fmt.Println("createTime:",createTime)
  567. dataTime := createTime.Format(utils.FormatDate)
  568. fmt.Println("dataTime:",dataTime)
  569. indexList := make([]*models.BaseFromOilchemIndex, 0)
  570. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  571. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  572. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  573. text3 := table3.Text()
  574. text3 = strings.Replace(text3,"\n","",-1)
  575. text3 = strings.Replace(text3," ","",-1)
  576. text3 = strings.Replace(text3,"\u00a0","",-1)
  577. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  578. return
  579. }
  580. //fmt.Println("table3:",text3)
  581. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  582. //fmt.Println("ii:",ii)
  583. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  584. //fmt.Println("jj:",jj)
  585. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  586. //fmt.Println("tableIndex:",tableIndex)
  587. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  588. if utils.ContainsChinese(text3) && text3 != "本周" {
  589. area = text3
  590. return
  591. }
  592. if area == "" {
  593. return
  594. }
  595. value := text3
  596. value = strings.TrimRight(value, "%")
  597. //valueF, e := strconv.ParseFloat(value, 64)
  598. //if e != nil {
  599. // err = e
  600. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  601. // return
  602. //}
  603. indexName := "中国甲醇" + area
  604. unit := ""
  605. if area == "产量" {
  606. unit = "吨"
  607. } else {
  608. unit = "%"
  609. }
  610. fmt.Println("indexName:",indexName)
  611. //fmt.Println("valueF:",valueF)
  612. fmt.Println("unit:",unit)
  613. item := &models.BaseFromOilchemIndex{
  614. IndexName: indexName,
  615. ClassifyId: 1,
  616. Unit: unit,
  617. Frequency: "周度",
  618. Describe: "",
  619. DataTime: dataTime,
  620. Value: value,
  621. Sort: 0,
  622. CreateTime: time.Now(),
  623. ModifyTime: time.Now(),
  624. IndexNameStr: indexName,
  625. }
  626. indexList = append(indexList,item)
  627. area = ""
  628. })
  629. })
  630. })
  631. // 写入数据库
  632. err = PostHandleOilchem(indexList)
  633. if err != nil {
  634. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  635. fmt.Println("PostHandleOilchem err",err)
  636. return
  637. }
  638. return
  639. }
  640. // 中国甲醇部分下游品种产能利用率
  641. func AnalysisOilchemJiaChun6(htm []byte) (err error) {
  642. if len(htm) == 0 {
  643. utils.FileLog.Info("htm empty")
  644. return
  645. }
  646. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  647. if e != nil {
  648. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  649. return
  650. }
  651. //titleList := make([]string, 0)
  652. //unitList := make([]string, 0)
  653. //doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  654. // ptext := selection.Text()
  655. // if strings.Contains(ptext, "趋势") {
  656. // return
  657. // }
  658. // if strings.Contains(ptext, "单位:") {
  659. // unit := strings.Replace(ptext, "单位:", "", -1)
  660. // //fmt.Println("unit:",unit)
  661. // unitList = append(unitList, unit)
  662. // }
  663. // if strings.Contains(ptext, "中国甲醇样本利润") {
  664. // title := ptext
  665. // //fmt.Println("title:",title)
  666. // titleList = append(titleList, title)
  667. // }
  668. //})
  669. area := ""
  670. title := doc.Find("h2").Text()
  671. fmt.Println("title:",title)
  672. createTimeStr := doc.Find("h2").Next().Text()
  673. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  674. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  675. createTimeStr = createTimeStr[:createTimeStrIndex]
  676. createTimeStr = strings.TrimSpace(createTimeStr)
  677. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  678. if err != nil {
  679. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  680. return
  681. }
  682. //fmt.Println("createTime:",createTime)
  683. dataTime := createTime.Format(utils.FormatDate)
  684. fmt.Println("dataTime:",dataTime)
  685. indexList := make([]*models.BaseFromOilchemIndex, 0)
  686. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  687. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  688. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  689. text3 := table3.Text()
  690. text3 = strings.Replace(text3,"\n","",-1)
  691. text3 = strings.Replace(text3," ","",-1)
  692. text3 = strings.Replace(text3,"\u00a0","",-1)
  693. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  694. return
  695. }
  696. //fmt.Println("table3:",text3)
  697. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  698. //fmt.Println("ii:",ii)
  699. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  700. //fmt.Println("jj:",jj)
  701. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  702. //fmt.Println("tableIndex:",tableIndex)
  703. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  704. if utils.ContainsChinese(text3) || utils.ContainsEnglishLetter(text3) {
  705. area = text3
  706. return
  707. }
  708. if area == "" {
  709. return
  710. }
  711. value := text3
  712. value = strings.TrimRight(value, "%")
  713. //valueF, e := strconv.ParseFloat(value, 64)
  714. //if e != nil {
  715. // err = e
  716. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  717. // return
  718. //}
  719. area = strings.Replace(area," ","",-1)
  720. area = strings.Replace(area," ","",-1)
  721. indexName := "中国甲醇部分下游品种产能利用率" +"("+ area +")"
  722. unit := "%"
  723. fmt.Println("indexName:",indexName)
  724. //fmt.Println("valueF:",valueF)
  725. fmt.Println("unit:",unit)
  726. item := &models.BaseFromOilchemIndex{
  727. BaseFromOilchemIndexId: 0,
  728. IndexName: indexName,
  729. ClassifyId: 1,
  730. Unit: unit,
  731. Frequency: "周度",
  732. Describe: "",
  733. DataTime: dataTime,
  734. Value: value,
  735. Sort: 0,
  736. CreateTime: createTime,
  737. ModifyTime: createTime,
  738. IndexNameStr: "中国甲醇部分下游品种产能利用率",
  739. MarketName: area,
  740. }
  741. indexList = append(indexList,item)
  742. area = ""
  743. })
  744. })
  745. })
  746. // 写入数据库
  747. err = PostHandleOilchem(indexList)
  748. if err != nil {
  749. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  750. fmt.Println("PostHandleOilchem err",err)
  751. return
  752. }
  753. return
  754. }
  755. // 中国甲醇样本生产企业库存及订单待发
  756. func AnalysisOilchemJiaChun7(htm []byte) (err error) {
  757. if len(htm) == 0 {
  758. utils.FileLog.Info("htm empty")
  759. return
  760. }
  761. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  762. if e != nil {
  763. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  764. return
  765. }
  766. //titleList := make([]string, 0)
  767. //unitList := make([]string, 0)
  768. //doc.Find("p").Each(func(i int, selection *goquery.Selection) {
  769. // ptext := selection.Text()
  770. // if strings.Contains(ptext, "趋势") {
  771. // return
  772. // }
  773. // if strings.Contains(ptext, "单位:") {
  774. // unit := strings.Replace(ptext, "单位:", "", -1)
  775. // //fmt.Println("unit:",unit)
  776. // unitList = append(unitList, unit)
  777. // }
  778. // if strings.Contains(ptext, "中国甲醇样本利润") {
  779. // title := ptext
  780. // //fmt.Println("title:",title)
  781. // titleList = append(titleList, title)
  782. // }
  783. //})
  784. area := ""
  785. title := doc.Find("h2").Text()
  786. fmt.Println("title:",title)
  787. createTimeStr := doc.Find("h2").Next().Text()
  788. createTimeStr = strings.TrimLeft(createTimeStr,"发布时间:")
  789. createTimeStrIndex := strings.Index(createTimeStr,"来源:")
  790. createTimeStr = createTimeStr[:createTimeStrIndex]
  791. createTimeStr = strings.TrimSpace(createTimeStr)
  792. createTime, err := time.Parse(utils.HlbFormatDateTimeNoSecond, createTimeStr)
  793. if err != nil {
  794. utils.FileLog.Error(fmt.Sprintf("time.Parse err:%v",err))
  795. return
  796. }
  797. //fmt.Println("createTime:",createTime)
  798. dataTime := createTime.Format(utils.FormatDate)
  799. fmt.Println("dataTime:",dataTime)
  800. indexList := make([]*models.BaseFromOilchemIndex, 0)
  801. doc.Find("tbody").Each(func(tableIndex int, table *goquery.Selection) {
  802. table.Find("tr").First().Each(func(ii int, table2 *goquery.Selection) {
  803. table.Find("td").Each(func(jj int, table3 *goquery.Selection) {
  804. text3 := table3.Text()
  805. text3 = strings.Replace(text3,"\n","",-1)
  806. text3 = strings.Replace(text3," ","",-1)
  807. text3 = strings.Replace(text3,"\u00a0","",-1)
  808. if text3 == "上周" || text3 == "环比" || text3 == "地区" {
  809. return
  810. }
  811. //fmt.Println("table3:",text3)
  812. //utils.FileLog.Info(fmt.Sprintf("table3:%s",text3))
  813. //fmt.Println("ii:",ii)
  814. //utils.FileLog.Info(fmt.Sprintf("ii:%d",ii))
  815. //fmt.Println("jj:",jj)
  816. //utils.FileLog.Info(fmt.Sprintf("jj:%d",jj))
  817. //fmt.Println("tableIndex:",tableIndex)
  818. //utils.FileLog.Info(fmt.Sprintf("tableIndex:%d",tableIndex))
  819. if utils.ContainsChinese(text3) || utils.ContainsEnglishLetter(text3) {
  820. area = text3
  821. return
  822. }
  823. if area == "" {
  824. return
  825. }
  826. value := text3
  827. value = strings.TrimRight(value, "%")
  828. //valueF, e := strconv.ParseFloat(value, 64)
  829. //if e != nil {
  830. // err = e
  831. // utils.FileLog.Error(fmt.Sprintf("strconv.ParseFloat err:%v",e))
  832. // return
  833. //}
  834. indexName := area
  835. unit := "万吨"
  836. fmt.Println("indexName:",indexName)
  837. //fmt.Println("valueF:",valueF)
  838. fmt.Println("unit:",unit)
  839. item := &models.BaseFromOilchemIndex{
  840. BaseFromOilchemIndexId: 0,
  841. IndexName: indexName,
  842. ClassifyId: 1,
  843. Unit: unit,
  844. Frequency: "周度",
  845. Describe: "",
  846. DataTime: dataTime,
  847. Value: value,
  848. Sort: 0,
  849. CreateTime: createTime,
  850. ModifyTime: createTime,
  851. IndexNameStr: indexName,
  852. MarketName: "",
  853. }
  854. indexList = append(indexList,item)
  855. area = ""
  856. })
  857. })
  858. })
  859. // 写入数据库
  860. err = PostHandleOilchem(indexList)
  861. if err != nil {
  862. utils.FileLog.Error(fmt.Sprintf("PostHandleOilchem err:%v",err))
  863. fmt.Println("PostHandleOilchem err",err)
  864. return
  865. }
  866. return
  867. }
  868. func PostHandleOilchem(indexList []*models.BaseFromOilchemIndex) (err error) {
  869. params := make(map[string]interface{})
  870. params["List"] = indexList
  871. result, e := postEdbLib(params, utils.LIB_ROUTE_OILCHEM_TABLE_HANDLE)
  872. if e != nil {
  873. b, _ := json.Marshal(params)
  874. fmt.Printf("postEdbLib err: %v, params: %s\n", e, string(b))
  875. utils.FileLog.Info(fmt.Sprintf("postEdbLib err: %v, params: %s", e, string(b)))
  876. return
  877. }
  878. resp := new(models.BaseEdbLibResponse)
  879. if e = json.Unmarshal(result, &resp); e != nil {
  880. fmt.Printf("json.Unmarshal err: %v\n", e)
  881. utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %v", e))
  882. return
  883. }
  884. if resp.Ret != 200 {
  885. fmt.Printf("Msg: %s, ErrMsg: %s\n", resp.Msg, resp.ErrMsg)
  886. utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  887. return
  888. }
  889. return
  890. }
  891. func JiaChunList(num int) (err error) {
  892. for k, v := range JiaChunListMap {
  893. for i := 1; i < num; i++ {
  894. listUrl := v + fmt.Sprintf("%d.html",i)
  895. fmt.Println("listUrl:",listUrl)
  896. htm, e := FetchPageHtml(listUrl)
  897. if e != nil {
  898. err = e
  899. utils.FileLog.Error(fmt.Sprintf("FetchPageHtml err:%v",err))
  900. fmt.Println("FetchPageHtml err",err)
  901. return
  902. }
  903. err = AnalysisOilchemList(htm, k)
  904. if err != nil {
  905. utils.FileLog.Error(fmt.Sprintf("AnalysisOilchemJiaChunList err:%v",err))
  906. fmt.Println("AnalysisOilchemJiaChunList err",err)
  907. return
  908. }
  909. //time.Sleep(2*time.Second)
  910. }
  911. }
  912. return
  913. }
  914. func AnalysisOilchemList(htm []byte, classifyTitle string) (err error) {
  915. if len(htm) == 0 {
  916. utils.FileLog.Info("htm empty")
  917. return
  918. }
  919. doc, e := goquery.NewDocumentFromReader(strings.NewReader(string(htm)))
  920. if e != nil {
  921. err = fmt.Errorf("NewDocumentFromReader err: %v", e)
  922. return
  923. }
  924. title := doc.Find("h2").Text()
  925. fmt.Println("title:",title)
  926. doc.Find("div.list").Each(func(divIndex int, table *goquery.Selection) {
  927. table.Find("li").Each(func(divIndex2 int, table2 *goquery.Selection) {
  928. title := table2.Text()
  929. if strings.Contains(title,classifyTitle) {
  930. table2.Find("li.clearfix a").Each(func(i int, s *goquery.Selection) {
  931. href, exists := s.Attr("href")
  932. if exists {
  933. fmt.Printf("Link %d: %s\n", i, href)
  934. respBody, err := FetchPageHtml(href)
  935. if err != nil {
  936. utils.FileLog.Error(fmt.Sprintf("FetchPageHtml err:%v",err))
  937. fmt.Println("FetchPageHtml err",err)
  938. return
  939. } else {
  940. handler, ok := OilchemTaskAnalysisHandlers[classifyTitle]
  941. if !ok {
  942. utils.FileLog.Info(fmt.Sprintf("%s无解析函数\n", classifyTitle))
  943. return
  944. }
  945. err := handler(respBody)
  946. if err != nil {
  947. utils.FileLog.Error(fmt.Sprintf("OilchemTaskAnalysisHandlers err:%v",err))
  948. fmt.Println("OilchemTaskAnalysisHandlers err",err)
  949. return
  950. }
  951. }
  952. } else {
  953. fmt.Println("Link attribute does not exist")
  954. }
  955. })
  956. }
  957. })
  958. })
  959. return
  960. }