Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 过滤推文中的图像_Python_Tweepy - Fatal编程技术网

Python 过滤推文中的图像

Python 过滤推文中的图像,python,tweepy,Python,Tweepy,我刚接触tweepy,我想知道如何跟踪并存储用户在其推文中发布的图像。我在教程中找到了几种获取用户推文的方法,但我找不到只过滤图像的方法 我使用以下代码来获取用户推文。如何仅获取用户图像 编辑:我像上面那样编辑我的代码: auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET) api = tweepy.API(auth) timeline

我刚接触tweepy,我想知道如何跟踪并存储用户在其推文中发布的图像。我在教程中找到了几种获取用户推文的方法,但我找不到只过滤图像的方法

我使用以下代码来获取用户推文。如何仅获取用户图像

编辑:我像上面那样编辑我的代码:

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET)
api = tweepy.API(auth)
timeline = api.user_timeline(count=10, screen_name = "zenitiss") 
for tweet in timeline: 
   for media in tweet.entities.get("media",[{}]):
      print media
      #checks if there is any media-entity
      if media.get("type",None) == "photo":
          # checks if the entity is of the type "photo"
          image_content=requests.get(media["media_url"])
          print image_content
然而,for循环似乎不起作用。打印媒体行打印空对象。基本上,当我试图打印用户的URL时,例如karyperry,我得到:

{u'url': u'http://t.co/TaP2JZrpxu', u'indices': [42, 64], u'expanded_url':  
u'http://youtu.be/7bDLIV96LD4', u'display_url': u'youtu.be/7bDLIV96LD4'}
{u'url': u'https://t.co/t3hv7VQiPG', u'indices': [42, 65], u'expanded_url': 
u'https://vine.co/v/MgvxZA2qKbV', u'display_url': u'vine.co/v/MgvxZA2qKbV'}
{u'url': u'http://t.co/vnJAAU7KN6', u'indices': [50, 72], u'expanded_url':
u'http://instagram.com/p/n01XZjv-fp/', u'display_url': u'instagram.com/p/n01XZjv-fp/'}
{u'url': u'http://t.co/NycqAwtcgo', u'indices': [78, 100], u'expanded_url':
u'http://bit.ly/1o7xQRj', u'display_url': u'bit.ly/1o7xQRj'}
{u'url': u'http://t.co/BG6ozuRD6D', u'indices': [111, 133], u'expanded_url':
u'http://www.johnnywujek.com/sos', u'display_url': u'johnnywujek.com/sos'}
{u'url': u'http://t.co/nWIQ9ruJ3f', u'indices': [88, 110], u'expanded_url':
u'http://uncf.us/1kSXIwF', u'display_url': u'uncf.us/1kSXIwF'}
{u'url': u'http://t.co/yTbOgqt9fw', u'indices': [101, 123], u'expanded_url':
u'http://instagram.com/p/nvxD8eP-SZ/', u'display_url': u'instagram.com/p/nvxD8eP-SZ/'}
大多数url都是图像,但是当我在tweet.entities.get(“url”,[{}])中的媒体循环中放入'url'而不是'media'时。大多数是图像URL。

Tweets(它们的JSON表示)包含一个“媒体”实体,如前所述。Tweepy应该按如下方式公开该类型的实体,假设推文中包含图像:

tweet.entities["media"]["media_url"]
因此,如果要存储图像,只需通过python的请求库下载即可。尝试向代码中添加类似以下语句的内容(或根据需要进行修改):


你能用图片发布推文的源代码吗?你说的推文源代码是什么意思?我指的是你得到的带有图片数据的推文..要从中提取图像,如果我打印tweet.text.encode(“utf-8”),它将包含带有URL的文本。你可以共享该文本。。由于inputI拥有alltweets对象,如何分别访问每条推文?
for media in tweet.entities.get("media",[{}]):
    #checks if there is any media-entity
    if media.get("type",None) == "photo":
        # checks if the entity is of the type "photo"
        image_content=requests.get(media["media_url"])
        # save to file etc.