倾销DOM使用Chrome无头和GO

问题描述 投票:0回答:1

What I am trying to do :

转储DOM是使用Chrome无头后呈现走。

我看到过aqautone的代码来获得一个想法,如何使用Chrome无头适合我的需要。

Problem

问题是,如果我添加多个网址,它只是超时打印Dumping dom timed out,目前如果只有两个URL它打印:

<html><head></head><body></body></html>
<html><head></head><body></body></html>
<html><head></head><body></body></html>
<html><head></head><body></body></html>
Checking  bingBot
1.73 URL : https://www.facebook.com
<html><head></head><body></body></html>
Checking  yahooBot
1.75 URL : https://www.facebook.com
Checking  bingBot
1.74 URL : https://www.google.com
Checking  googleBot
1.75 URL : https://www.google.com
Checking  googleBot
1.76 URL : https://www.facebook.com
<html><head></head><body></body></html>
Checking  yahooBot
1.76 URL : https://www.google.com
Total time taken 1.78s elapsed

这里是我的Go代码:Playground(但是因为没有铬高管有那么它是没有意义的)

如果你不想去游乐场:

package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "os"
    "os/exec"
    "sync"
    "time"
    //"github.com/remeh/sizedwaitgroup"
)

// Taken from https://deviceatlas.com/blog/list-of-user-agent-strings
var (
    useragentstrings = map[string]string{
        "googleBot": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
        "bingBot":   "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
        "yahooBot":  "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)",
    }
    chromePath = "/root/tools/chromium-latest-linux/latest/chrome"
)

func fetchUsingChrome(url string, nameOfAgent string, useragent string, ch chan<- string, wg *sync.WaitGroup) {
    // func fetchUsingChrome(url string, nameOfAgent string, useragent string, ch chan<- string, wg *sizedwaitgroup.SizedWaitGroup) {
    start := time.Now()
    defer wg.Done()
    var chromeArguments = []string{
        "--headless", "--disable-gpu", "--hide-scrollbars", "--mute-audio", "--disable-notifications",
        "--disable-crash-reporter",
        "--ignore-certificate-errors",
        "--dump-dom",
        // "--screenshot=" + url + ".png",
        "--user-agent=" + useragent,
    }
    if os.Geteuid() == 0 {
        chromeArguments = append(chromeArguments, "--no-sandbox")
    }
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    cmd := exec.CommandContext(ctx, chromePath, chromeArguments...)
    stdout, _ := cmd.StdoutPipe()
    if err := cmd.Start(); err != nil {
        fmt.Printf("[%s]Dumping dom failed Error: %v\n", err)
        return
    }
    // buf := new(bytes.Buffer)

    // if err := buf.ReadFrom(stdout); err != nil {
    // fmt.Println(err)
    // return
    // }
    // fmt.Println(buf.String())
    if _, err := io.Copy(os.Stdout, stdout); err != nil {
        log.Fatal(err)
    }

    if err := cmd.Wait(); err != nil {
        if ctx.Err() == context.DeadlineExceeded {
            fmt.Printf("Dumping dom timed out : %s\n", url)
            return
        }
        fmt.Printf("[%s]Dumping dom failed Error: %v\n", err)
        return
    }
    secs := time.Since(start).Seconds()
    fmt.Println("Checking ", nameOfAgent)
    ch <- fmt.Sprintf("%.2f URL : %s", secs, url)
}

func main() {
    var wg sync.WaitGroup
    output := []string{
        "https://www.facebook.com",
        "https://www.google.com",
    }
    start := time.Now()
    ch := make(chan string)
    //swg := sizedwaitgroup.New(10)
    for _, url := range output {
        for k, v := range useragentstrings {
            wg.Add(1)
            //swg.Add()
            go fetchUsingChrome(url, k, v, ch, &wg)
            // go fetchUsingChrome(url, k, v, ch, &swg)
        }
    }

    go func() {
        wg.Wait()
        // swg.Wait()
        close(ch)
    }()
    for val := range ch {
        fmt.Println(val)
    }
    fmt.Printf("Total time taken %.2fs elapsed\n", time.Since(start).Seconds())
}

附: :我故意留下了一些注释代码,给见识,什么都我已经尝试和失败。

谢谢 临时 (A golang noobie)

go google-chrome-headless
1个回答
0
投票

我会建议开始铬无头,然后用Chrome DevTools Protocol控制你在本地机器上打开了实例,但它也可以与远程铬​​实例。

你可以使用这个库:https://github.com/chromedp/chromedp

我用这个做网页的Chrome无头,我在泊坞窗打开容器的截图。

这里是什么,我做了一些例子:https://gist.github.com/efimovalex/9f9b815b0d5b1b7889a51d46860faf8a

© www.soinside.com 2019 - 2024. All rights reserved.