问题
I have some fundamental questions about injectFile:
- When do you recomment using injectFile(code) instead of page.evaluate(code, ...) to get code into the page to be scraped?
- What are the differences/advantages/disadvantages in using injectFile(code), for example in performance, anti-scraping-detectability or the like?
- How should injectFile(code) be implemented, by replacing gotoFunction and implementing page.on('domcontentloaded', ...) event, like I did? Or is there a better way?
- 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