書き込みクローラはchromedpウェブサイトのログインとクロールコンテンツと言語の実現の枠組みの下で行きます

避けて迂回するために、ウェブサイト、荒廃、自動ログオンchromedpの開発とコンテンツクロールプロセスを使用する際に一連の問題に直面して、後で、単に何を全体の実装プロセスを記録します。

JSエラー問題:

クロムは、ログインページを開くと、次のヒントは、jQueryのフレームワークは、ファイルを見つけることができません。

私たちは、次のように解決策があり、サイトのjsエラーで独自のコードの修正を記述する必要があります。

var executed *runtime.RemoteObject
err = chromedp.Run(ctx, chromedp.Tasks{
		chromedp.Evaluate(`var jq = document.createElement('script'); jq.src = "https://cdn.bootcss.com/jquery/1.4.2/jquery.js"; document.getElementsByTagName('head')[0].appendChild(jq);`,&executed),
})

またはコードこれで他の直接見て、

package main

import (
	"context"
	"github.com/chromedp/cdproto/emulation"
	"github.com/chromedp/cdproto/runtime"
	"github.com/chromedp/chromedp"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

func main()  {
	var buf []byte

	// create chrome instance
	ctx, cancel := chromedp.NewContext(
		context.Background(),
		chromedp.WithDebugf(log.Printf),
	)
	defer cancel()

	// create a timeout
	ctx, cancel = context.WithTimeout(ctx, 50*time.Second)
	defer cancel()

	// run task list
	var res string
	var err error

	width, height := 1920, 1080
	err=chromedp.Run(ctx, chromedp.Tasks{
		emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
	})

	loginUrl:=`http://a.b.c.d/login`

	var executed *runtime.RemoteObject
	username:="yourusername"
	password:="yourpassword"
	err = chromedp.Run(ctx, chromedp.Tasks{
		chromedp.Navigate(loginUrl),
		chromedp.Sleep(5 * time.Second),
		chromedp.Evaluate(`var jq = document.createElement('script'); jq.src = "https://cdn.bootcss.com/jquery/1.4.2/jquery.js"; document.getElementsByTagName('head')[0].appendChild(jq);`,&executed),
		chromedp.Sleep(5 * time.Second),
		chromedp.WaitVisible(`#mypassword`, chromedp.ByID),
		chromedp.SendKeys (`input[name="username"]`, username,chromedp.NodeVisible),
		chromedp.SendKeys(`#mypassword`, password, chromedp.ByID),
		chromedp.Sleep(2 * time.Second),
		chromedp.Click(`#login_btn`, chromedp.ByID),
		chromedp.Sleep(5 * time.Second),
		chromedp.CaptureScreenshot(&buf),
	})

	if err != nil {
		log.Fatal(err)
	}
	if err := ioutil.WriteFile("1.png", buf, 0644); err != nil {
		log.Fatal(err)
	}

	indexPageUrl:=`http://a.b.c.d/somepage`

	err = chromedp.Run(ctx, chromedp.Tasks{
		chromedp.Navigate(indexPageUrl),
		chromedp.Sleep(5 * time.Second),
		chromedp.CaptureScreenshot(&buf),
		chromedp.WaitVisible(`#somehtmlid`,chromedp.ByID),
	})
	if err != nil {
		log.Fatal(err)
	}
	if err := ioutil.WriteFile("2.png", buf, 0644); err != nil {
		log.Fatal(err)
	}

	log.Printf("got: `%s`", strings.TrimSpace(res))

}

 

公開された177元の記事 ウォン称賛21 ビュー500 000 +

おすすめ

転載: blog.csdn.net/peihexian/article/details/104436496