javascript 从 9GAG 拉取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9345705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:21:02  来源:igfitidea点击:

Pulling Images from 9GAG

javascriptjqueryhtmlcss

提问by n0shadow

Is there any way that I can use JavaScript (and jQuery) to create a webpage which shows only the images from 9GAG, loads about 5 at a time, and loads more as one scrolls down? I'm trying to make an easier way for myself to access such websites without loading all of the other garbage.

有什么方法可以使用 JavaScript(和 jQuery)创建一个网页,该网页只显示来自 9GAG 的图像,一次加载大约 5 个,并且在向下滚动时加载更多?我正在尝试为自己提供一种更简单的方式来访问这些网站,而无需加载所有其他垃圾。

Thanks.

谢谢。

回答by k3min

Take a look at InfiniGAG.

看看InfiniGAG

http://infinigag.k3min.eu/trending

http://infinigag.k3min.eu/trending

Returns:

返回:

{
    "status": 200,
    "message": "OK",
    "data": [
        {
            "id": "EyVtjpq",
            "caption": "Example",
            "images": {
                "small": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_220x145.jpg",
                "cover": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460c.jpg",
                "normal": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460s.jpg",
                "large": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_700b.jpg"
            },
            "media": {
                "mp4": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460sv.mp4",
                "webm": "http:\/\/img-9gag-fun.9cache.com\/photo\/EyVtjpq_460svwm.webm"
            },
            "link": "http:\/\/9gag.com\/gag\/EyVtjpq",
            "votes": {
                "count": 0
            },
            "comments": {
                "count": 0
            }
        },
        ...
    ],
    "paging": {
        "next": "V8eFpqG"
    }
}

回答by hackartist

Yes this is possible... I would suggest using php. Unfortunately you won't be able to do this with only javascript because of "sandboxing" where you can only access things stored from the url the javascript originates from.

是的,这是可能的...我建议使用 php。不幸的是,您将无法仅使用 javascript 执行此操作,因为“沙箱”您只能访问从 javascript 源自的 url 存储的内容。

Here is how you can do it below. With this code comes some advice: TOS are just rules, but understand the moral/ethical considerations involved when/if using this. 9gag did the work of gathering these images so that they can get money on advertising. They pay for bandwidth (and additionally for the cloudfront cdn). If you were to use this code to make a competing product or even just to circumnavigate the ads then you are stealing. I only post this because I think that information should be free and people should be able to learn from this php code.

以下是您如何做到这一点。此代码提供了一些建议:TOS 只是规则,但要了解使用时/如果使用它时涉及的道德/伦理考虑。9gag 完成了收集这些图像的工作,以便他们可以通过广告赚钱。他们为带宽付费(另外还为 cloudfront CDN 付费)。如果您要使用此代码来制作竞争产品,甚至只是为了绕过广告,那么您就是在偷窃。我发布这个只是因为我认为信息应该是免费的,人们应该能够从这个 php 代码中学习。

$html = file_get_contents("http://www.9gag.com");
preg_match('|http://d24w6bsrhbeh9d\.cloudfront\.net/photo/.+?\.jpg|',$html,$matches);
//now matches holds all the image urls which you can print to javascript/html and show

Good luck.

祝你好运。

回答by Kevin B

Unless said websites have an api that returns the images or paths to them, you will need server-side technology for this.

除非所述网站具有返回图像或路径的 api,否则您将需要服务器端技术。