jiachun_edb.go 33 KB

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