下载imgur图片的顶级评论(jpg)

时间:2016-10-18 00:58:19

标签: python json web-scraping imgur

如何使用json访问与imgur图像相关的注释?

对于http://imgur.com/gallery/DVNWyG8,浏览到http://api.imgur.com/3/image/DVNWyG8.json会产生:

{"data":{"id":"DVNWyG8","title":"Fiber optic dress","description":null,"datetime":1476709283,"type":"image\/jpeg","animated":false,"width":2056,"height":1694,"size":482983,"views":1883274,"bandwidth":909589326342,"vote":null,"favorite":false,"nsfw":false,"section":"pics","account_url":null,"account_id":null,"is_ad":false,"in_gallery":true,"link":"http:\/\/i.imgur.com\/DVNWyG8.jpg"},"success":true,"status":200}

此外,我无法忘记它,因为我收到此错误:

wget http://api.imgur.com/3/image/DVNWyG8.json
--2016-10-17 19:55:48--  http://api.imgur.com/3/image/DVNWyG8.json
Resolving api.imgur.com (api.imgur.com)... 151.101.44.193
Connecting to api.imgur.com (api.imgur.com)|151.101.44.193|:80... connected.
HTTP request sent, awaiting response... 400 Bad Request
2016-10-17 19:55:48 ERROR 400: Bad Request.

基本上我想下载一张带有前30条评论的Imgur图片。我想知道我是否应该用它的json做一些事情,或者是否有一个巧妙的Python API用于此目的?

即使使用API​​,我也没有看到获取与图像相关的所有注释的方法:

  9 imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
 10 
 11 image = imgur_client.get_image("S1jmapR")
 12 print(image.title)
 13 print(image.link)
 14 print(dir(image))

我明白了:

mona@pascal:~/computer_vision/imgur$ python download.py 
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Cat Ying & Yang
http://i.imgur.com/S1jmapR.jpg
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'account_id', 'account_url', 'animated', 'bandwidth', 'datetime', 'description', 'favorite', 'height', 'id', 'in_gallery', 'is_ad', 'link', 'nsfw', 'section', 'size', 'title', 'type', 'views', 'vote', 'width']
mona@pascal:~/computer_vision/imgur$ vi download.py

如您所见,图片没有评论方法或get_comment。

2 个答案:

答案 0 :(得分:1)

 21 for item in imgur_client.gallery_item_comments("c1SN8", sort='best'):
 22     print item.comment

将完成这项工作

答案 1 :(得分:0)

这个python库适合您的需求,https://github.com/Imgur/imgurpython