如何使用Tweepy Tweet Media并在Python中获取请求?

发布于 2025-02-12 01:33:21 字数 330 浏览 2 评论 0原文

这就是我键入的内容,它打印了一个错误,上面写着:value eRror:stat:eNuld null字符

任何人都可以帮助解释我做错了什么以及我应该如何修复它?

    media = requests.get(url, stream = True)
    api.media_upload(media)
    with open(media.jpg, 'wb') as f:
                f.write(media.content)

This is what I typed, and it printed an error that says: ValueError: stat: embedded null character in path

Could anyone help explain what I did wrong and how I should fix it?

    media = requests.get(url, stream = True)
    api.media_upload(media)
    with open(media.jpg, 'wb') as f:
                f.write(media.content)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-02-19 01:33:22

您可以使用urllib模块,而不是请求模块。

示例:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET,
)
auth = tweepy.OAuth1UserHandler(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)

# Create tweet
message = " MESSAGE "

# Upload image
img_url = " IMAGE URL "
urllib.request.urlretrieve(img_url, "tweet_img.png")
media = api.media_upload("tweet_img.png")

# Post tweet with image
client.create_tweet(text=message, media_ids=[media.media_id])
print("Tweeted!")

我希望这会有所帮助。

You can use the urllib module instead of the requests module.

Example:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET,
)
auth = tweepy.OAuth1UserHandler(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)

# Create tweet
message = " MESSAGE "

# Upload image
img_url = " IMAGE URL "
urllib.request.urlretrieve(img_url, "tweet_img.png")
media = api.media_upload("tweet_img.png")

# Post tweet with image
client.create_tweet(text=message, media_ids=[media.media_id])
print("Tweeted!")

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文