Fundamental questions about injectFile

怎甘沉沦 提交于 2020-01-16 09:07:25

问题


I have some fundamental questions about injectFile:

  1. When do you recomment using injectFile(code) instead of page.evaluate(code, ...) to get code into the page to be scraped?
  2. What are the differences/advantages/disadvantages in using injectFile(code), for example in performance, anti-scraping-detectability or the like?
  3. How should injectFile(code) be implemented, by replacing gotoFunction and implementing page.on('domcontentloaded', ...) event, like I did? Or is there a better way?
  4. What is the use-case of the option 'surviveNavigations', if not the one described in my previous post? What is it for?

Best regards, Wolfgang


回答1:


If you look at the short source code of injectFile, it uses page.evaluate so they are both basically the same.

1) We use almost always page.evaluate simply because it is less code. If we need to use some more complex libraries, we use them in the Node.js process (server-side) instead of injecting it into the page. We use the injection only for JQuery.

2) It doesn't matter for performance or anti-scraping.

3) Usually, we inject in handlePageFunction but if you need it before navigation, you can inject inside gotoFunction.

4) For example if you click on the button, the page can navigate. But for a new page/request, you should inject it again. As I said, unless you use some gigantic library, it doesn't have any performance impact.



来源:https://stackoverflow.com/questions/58745103/fundamental-questions-about-injectfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!