Golang 模拟页面访问:Surf 不触发 JavaScript。

huangapple go评论66阅读模式
英文:

Golang Simulate Page Visit : Surf Not Triggering Javascript

问题

我有一个使用Surf(gopkg.in/headzoo/surf.v1)模拟访问本地网页的Go程序,但似乎页面上的JavaScript没有被执行。为了测试,我让网页向服务器的另一个路由发起ajax调用,服务器将在服务器端输出响应。

<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<script>
$.ajax ({
    url : "/test",
    type : "GET",
    success : function(res) {},
    error : function(res) {}
});
</script>
</body>
</html>

然后是用于访问页面的Go Surf函数,该路由也在服务器端输出响应,但ajax请求的响应没有显示出来。

func simulateVisit() {

    bow := surf.NewBrowser()
    bow.SetUserAgent("LocalCrawler/1.0")

    err := bow.Open("http://127.0.0.1/page")
    if err != nil { fmt.Println(err) }

}

为了确保这只发生在Surf上,我还使用了Chromedp(github.com/chromedp/chromedp),它正确地返回了两个响应。

这是Surf的限制还是我做了什么愚蠢的事情?

英文:

I have a Go program that is just simulating a local web page being visited using Surf (gopkg.in/headzoo/surf.v1) but it seems that the Javascript on the page is not being executed. Just to test I have the web page doing an ajax call to another route on the server that will just output a response on the server side.

&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script&gt;
$.ajax ({
    url : &quot;/test&quot;,
    type : &quot;GET&quot;,
    success : function(res) {},
    error : function(res) {}
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Then the Go Surf function to visit the page, this route also outputs a response on the server side which is showing up but the one from the ajax request is not showing up.

func simulateVisit() {

    bow := surf.NewBrowser()
    bow.SetUserAgent(&quot;LocalCrawler/1.0&quot;)

    err := bow.Open(&quot;http://127.0.0.1/page&quot;)
    if err != nil { fmt.Println(err) }

}

To make sure it is only happening from Surf I also tested it with Chromedp (github.com/chromedp/chromedp) which gave both responses correctly.

Is this a limitation with Surf or am I doing something silly?

答案1

得分: 4

Surf不包含JS运行时。它支持下载脚本,但不会实际执行它们。

英文:

Surf does not include a JS runtime. It does support the downloading of scripts, but it will never actually execute them.

huangapple
  • 本文由 发表于 2022年1月6日 04:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/70599473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定