简体   繁体   English

通过javascript从网站获取动态内容

[英]Getting dynamic content from a website through javascript

I am creating a firefox plugin and using javascript. 我正在创建一个firefox插件并使用javascript。 I am using XMLHttpRequest to get dynamic content from a remote website and storing it into a file to parse it later. 我正在使用XMLHttpRequest从远程网站获取动态内容,并将其存储到文件中以供日后解析。 But in responseText i am not receiving any dynamic content. 但是在responseText中,我没有收到任何动态内容。 After storing information that i got is only the html page not having dynamic content. 存储信息后,我得到的只是html页面没有动态内容。

code: 码:

var res="";

var req = new XMLHttpRequest();

req.onload = function(){

res=this.responseText;
}   

req.open("GET","www.ebay.com", true);

req.send();

You can only fetch remote content under your own domain. 您只能在自己的域下获取远程内容。 This is a security issue. 这是一个安全问题。 You can use JSONP , But for ebay I don't know if there are any json contents available. 您可以使用JSONP ,但是对于ebay,我不知道是否有可用的json内容。 for more information have a look at this: http://www.devproconnections.com/article/aspnet2/ajax-cross-domain-142169 有关更多信息, 请参见http : //www.devproconnections.com/article/aspnet2/ajax-cross-domain-142169

In the case you are using Javascript with a Chrome extension or a Firefox add-on, then you must set permissions in your manifest file to have access to those sites you want. 如果您将Javascript与Chrome扩展程序或Firefox附加组件一起使用,则必须在清单文件中设置权限才能访问所需的那些网站。

It's can't be done due to same-origin policy , so you can only request content within the origin domain. 由于同源政策 ,无法完成此操作,因此您只能在原始域内请求内容。

Here are some links may be helpful to you: 以下一些链接可能对您有帮助:
Getting CORS Working 让CORS工作
Cross-domain Ajax with Cross-Origin Resource Sharing 具有跨域资源共享的跨域Ajax


Edit: 编辑:
Since what you want to get from a website is generated by Ajax , so it's impossible to get the dynamic content. 由于您想从网站获得的内容是由Ajax生成的,因此无法获得动态内容。 the dynamic content present on the website is after the browser loaded the html page and so the Javascript Event can be triggered to get the dynamic content through Ajax . 网站上显示的dynamic content是在浏览器加载html页面之后进行的,因此可以触发Javascript Event通过Ajax获取dynamic content You can get the html page, but you didn't get the Javascript files. 可以获取html页面,但未获取Javascript文件。 therefor, Can't get the dynmic content. 因此, 无法获得动态内容。

sorry for my bad English 对不起,我的英语不好

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

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