fgenergy.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_crawler_local/utils"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "github.com/chromedp/chromedp"
  12. )
  13. func autoLogin() {
  14. fmt.Println("task start")
  15. loginUrl := `https://www.fgenergy.com`
  16. userName := "lizhan.edri@sinopec.com"
  17. pwd := "Lzhan@@123"
  18. opts := append(
  19. chromedp.DefaultExecAllocatorOptions[:],
  20. chromedp.Flag("headless", false),
  21. )
  22. allocCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
  23. //defer cancel()
  24. // 创建chrome实例
  25. ctx, _ := chromedp.NewContext(
  26. allocCtx,
  27. chromedp.WithLogf(log.Printf),
  28. )
  29. err := chromedp.Run(ctx,
  30. chromedp.Navigate(loginUrl),
  31. chromedp.Sleep(5*time.Second),
  32. chromedp.Click(`a[id="lnkClientLogin"]`, chromedp.ByQuery),
  33. chromedp.Sleep(2*time.Second),
  34. chromedp.SetValue(`input[id="ContentPlaceHolderDefault_MemberLogin_NEW_2_m_login_UserName"]`, userName, chromedp.ByQuery),
  35. chromedp.SetValue(`input[id="ContentPlaceHolderDefault_MemberLogin_NEW_2_m_login_Password"]`, pwd, chromedp.ByQuery),
  36. chromedp.Sleep(2*time.Second),
  37. chromedp.Click(`input[id="ContentPlaceHolderDefault_MemberLogin_NEW_2_m_login_LoginButton"]`, chromedp.ByQuery),
  38. chromedp.Sleep(5*time.Second),
  39. )
  40. fmt.Println(err)
  41. time.Sleep(3 * time.Second)
  42. //自动登录之后,拿到cookie
  43. autoDownload()
  44. fmt.Println("task end")
  45. }
  46. func autoDownload() {
  47. url := "https://www.fgenergy.com/download/51635/FGE+-+Fuel+Oil+Alert+-+18+December+2024.pdf"
  48. method := "GET"
  49. payload := strings.NewReader(``)
  50. client := &http.Client{}
  51. req, err := http.NewRequest(method, url, payload)
  52. if err != nil {
  53. fmt.Println(err)
  54. return
  55. }
  56. req.Header.Add("cookie", "ASP.NET_SessionId=10zktz2ng0zjt31y1b2jmrzg; _gid=GA1.2.832819655.1735175796; _lfa=LF1.1.cc68a12187105a86.1735175796600; hubspotutk=420d6b24dbf885d44e7b7a39127fc66e; __hssrc=1; cebs=1; _ce.clock_data=-1242%2C58.152.60.223%2C1%2Cf51bb482c660d0eeadd1f058058a2b35%2CChrome%2CHK; yourAuthCookie=7EC2C20F11960F813492FFE8E028D9A61E2FE22A2D8B9E2BA941EE56F404F1B4C5E134E26DBCA84BACEF0CF5930E1AE3E71DB4C0DC42F56687B0C3A4658A66073AA74AA5F85C3ECF0DF8F651276916338263F76BB409AF6EBAB7A763E344F1682CE37CDBEAA0A07C5ABC36250B6B44A848B9E4F24630CBEF1EB8D7C6E88E7287; _ga=GA1.2.809660828.1735175796; __hstc=229974915.420d6b24dbf885d44e7b7a39127fc66e.1735175797800.1735183452658.1735190316139.4; __hssc=229974915.1.1735190316139; cebsp_=13; _ga_YJLTHWFMBM=GS1.1.1735190312.4.1.1735190427.60.0.0; _ce.s=v~f971c41dae7a80ad9ab608fea61febb8d9592e02~lcw~1735191678959~vir~new~lva~1735175800122~vpv~0~v11.fhb~1735175801735~v11.lhb~1735191679319~v11.cs~416135~v11.s~dc300190-c348-11ef-a2bc-31da31de46a1~v11.sla~1735191678959~v11.send~1735190346263~lcw~1735191679319")
  57. res, err := client.Do(req)
  58. if err != nil {
  59. fmt.Println(err)
  60. return
  61. }
  62. defer res.Body.Close()
  63. body, err := io.ReadAll(res.Body)
  64. if err != nil {
  65. fmt.Println(err)
  66. return
  67. }
  68. fmt.Println(string(body))
  69. utils.SaveToFile(string(body), "1.pdf")
  70. }