简体   繁体   English

jQuery-获取HTTP请求未在获取的源中执行JavaScript

[英]jQuery - Get HTTP request doesn't execute JavaScript in fetched source

My goal is to create a Chrome plug-in that fetches a webpage from a given URL and scrapes it's HTML content to find a download link to a video. 我的目标是创建一个Chrome插件,该插件从给定的URL提取网页并抓取其HTML内容,以找到视频的下载链接。 The problem is that the fetched webpage runs some JavaScript to generate download links on the page. 问题在于,获取的网页运行一些JavaScript来生成页面上的下载链接。 However, when I fetch the page using below jQuery code, I don't see the download URL as (possibly) the JavaScript on the fetched page hasn't executed. 但是,当我使用下面的jQuery代码获取页面时,我没有看到下载URL,因为(可能)获取的页面上的JavaScript尚未执行。

$.get(link, function(response) {
    console.log($response);
    //console.log($(response).find(".videoData").attr("value"));
});

I looked online and got to know that jQuery's .load() function will also fail in this case. 我在网上看了一下,才知道jQuery的.load()函数在这种情况下也会失败。 PhantomJS's page.open() function seems to do what I require, but I want to deploy some lightweight solution inside my Chrome extension, so I am unable to use PhantomJS. PhantomJS的page.open()函数似乎可以满足我的要求,但是我想在Chrome扩展程序中部署一些轻量级的解决方案,因此我无法使用PhantomJS。

You can inject a Content Script in the web page you're trying to scrape. 您可以在要抓取的网页中插入内容脚本 Make sure that you enter "document_end" in the manifest so you can ensure that your script executes after the target page has finished loading. 确保在清单中输入“ document_end”,以便可以确保在目标页面完成加载执行脚本。

Another solution would be to create an invisible iframe within which you'd load the target url, and then scrape the loaded code for the video links. 另一种解决方案是创建一个不可见的iframe在其中加载目标网址,然后抓取已加载的video链接代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM