KeyError:“边缘\媒体\至\赞助商\用户”

2024-09-30 10:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用instaloader从instagram中提取信息。我的代码运行时基本没有问题,但使用“is_sponosed”功能似乎会导致错误,但只有当“is_sponosed”为真时才会出现错误? `

  ## this is a snippet of the code I think is relevant, not the full thing.


    posts = instaloader.Profile.from_username(instagram.context, company).get_posts()

    SINCE = datetime(2020, 12, 5)
    UNTIL = datetime(2020, 1, 1)

    processed = 1
    for post in takewhile(lambda p: p.date > UNTIL, dropwhile(lambda p: p.date > SINCE, posts)):

        print(post.date)
        print("...scraping info for post %s, %s" % (post.shortcode, company))




        post_info = {
            "shortcode": post.shortcode,
            "username": company,
            "date_utc": post.date_utc.strftime('%Y-%m-%d %H:%M:%S.%f'),
            "is_video": "yes" if post.is_video else "no",
            "is_sponsored": post.is_sponsored,
            "hashtags": (",".join(post.caption_hashtags)).encode('utf-8', errors='ignore'),
            "mentions": (",".join(post.caption_mentions)).encode('utf-8', errors='ignore'),
            "caption": (emoji.demojize(post.caption)).encode('utf-8', errors='ignore') if post.caption else "",
            "video_view_count": post.video_view_count if post.is_video else 0,
            "video_length": post.video_duration if post.is_video else 0,
            "likes": post.likes,
            "comments": post.comments,
            "location_name": (post.location.name).encode('utf-8', errors='ignore') if post.location else "",
            "location_latlong": " ".join((str(post.location.lat), str(post.location.lng))) if post.location else ""
            }

        processed += 1
        post_writer.writerow(post_info)

    print("...scraped %i posts for %s" % (processed - 1, company))`

但我时不时地会发现这个错误,与is\U赞助有关。

Traceback (most recent call last):
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\structures.py", line 179, in _field
    d = d[key]
KeyError: 'edge_media_to_sponsor_user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "post-scraper.py", line 90, in <module>
    "is_sponsored": post.is_sponsored,
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\structures.py", line 464, in is_sponsored
    sponsor_edges = self._field('edge_media_to_sponsor_user', 'edges')
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\structures.py", line 182, in _field
    d = self._full_metadata
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\structures.py", line 161, in _full_metadata
    self._obtain_metadata()
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\structures.py", line 144, in _obtain_metadata
    pic_json = self._context.graphql_query(
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\instaloadercontext.py", line 418, in graphql_query
    resp_json = self.get_json('graphql/query',
  File "C:\Users\madel\anaconda3\lib\site-packages\instaloader\instaloadercontext.py", line 335, in get_json
    raise QueryReturnedBadRequestException("400 Bad Request")
instaloader.exceptions.QueryReturnedBadRequestException: 400 Bad Request

当我查看我所收集的数据时,似乎所有的“赞助”都是“虚假的”。发生了什么事?如何更改代码以阻止这种情况发生?


Tags: inpyislibpackagesvideolinesite

热门问题