开发者社区> 问答> 正文

用Java 怎样实现一个获取js加载完全之后的html文件呢??报错

我已用过httpclient htmlparser 获取到的都是未加载的,用htmlunit会报错,一个是直接打印Your browser does not support the canvas element.另外一个是报错EcmaError: lineNumber=[7986] column=[0] lineSource=[null] name=[TypeError] sourceName=[http://yx8.com/game/paopaolixianji/game.js] message=[TypeError: Cannot read property "width" from undefined (http://yx8.com/game/paopaolixianji/game.js#7986)]....后面还有一大串类似这种。 我的本意是想抓取一些单机版的网页游戏手动另存为这种方式感觉太繁琐太慢了。 请问大神们有什么好的解决方案吗?

展开
收起
爱吃鱼的程序员 2020-06-09 15:42:01 860 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    我都是直接用Runtime.exec方法执行外部一个phantomjs( http://www.oschina.net/p/phantomjs )的js脚本来把网页内容输出到stdout,再从java里读。js脚本如下:


    varwebpage=require('webpage');varsystem=require('system');varpage=webpage.create();page.open(system.args[1],function(status){if(status==='success'){varcontent=page.evaluate(function(){returndocument.documentElement.outerHTML;});system.stdout.write(content);}phantom.exit();});




    回复 @JasonO:你可以试试在那个if里试试延迟几秒,或者随便模拟点事件之类的我刚试了一下,还是不行呢。不能获取js动态添加html属性之后的html~呜呜谢谢,请问我爬那些html5的游戏也可以吗?这个有现成的工具的吧请问有没有好的工具介绍一下呢

    这个很难吧,需要模拟网页的执行

    webdriver?

    IE11

     

    2020-06-09 15:42:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载