source_trade_zhengzhou.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package services
  2. import (
  3. "context"
  4. "github.com/chromedp/chromedp"
  5. "log"
  6. "time"
  7. )
  8. func getSourceZhengZhou(url string) (string, error) {
  9. //增加选项,允许chrome窗口显示出来
  10. options := []chromedp.ExecAllocatorOption{
  11. //chromedp.Flag("headless", false),
  12. chromedp.Flag("disable-blink-features","AutomationControlled"),
  13. chromedp.UserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36`),
  14. }
  15. options = append(chromedp.DefaultExecAllocatorOptions[:], options...)
  16. //创建chrome窗口
  17. allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)
  18. defer cancel()
  19. newctx, cancel := chromedp.NewContext(allocCtx)
  20. defer cancel()
  21. // 给每个页面的爬取设置超时时间
  22. ctx, cancel := context.WithTimeout(newctx, 200 * time.Second)
  23. defer cancel()
  24. // run task list
  25. var res string
  26. err := chromedp.Run(ctx,
  27. chromedp.Tasks{
  28. /*chromedp.ActionFunc(func(cxt context.Context) error {
  29. _, err := page.AddScriptToEvaluateOnNewDocument("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})").Do(cxt)
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }),*/
  35. chromedp.Navigate(url),
  36. chromedp.Sleep(5*time.Second),
  37. chromedp.InnerHTML("body", &res),
  38. //chromedp.EvaluateAsDevTools(`document.querySelector("#left > iframe").contentWindow.document.body.outerHTML;`, &res),
  39. },
  40. )
  41. if err != nil {
  42. log.Print(err)
  43. return "", err
  44. }
  45. return res, nil
  46. }