我目前正在编写一个 Twitter 机器人,它只是使用 Python 包装器“Twikit”创建带有几个媒体的推文。 这是我写的Python代码“betsuaka_test.py”如下:
from twikit import Client
client = Client('ja')
client.login(
auth_info_1='xxxxxxxxx',
auth_info_2='[email protected]',
password='xxxxxxxx'
)
TWEET_TEXT = 'テスト投稿2'
MEDIA_IDS = [
client.upload_media('https://i.imgur.com/ZFMaW5h.png',0)
]
client.create_tweet(TWEET_TEXT, MEDIA_IDS)
当我使用
python3 betsuaka_test.py
运行此代码时,我的 shell 交替返回 *两种类型的错误消息:
[错误信息1]
ubuntunaoki@LAPTOP-7Q7QL2PR:~$ python3 betsuaka_test.py
Traceback (most recent call last):
File "/home/ubuntunaoki/betsuaka_test.py", line 13, in <module>
client.upload_media('https://i.imgur.com/ZFMaW5h.png',0)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/twikit/client.py", line 568, in upload_media
img_size = os.path.getsize(source)
File "/usr/lib/python3.10/genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: 'https://i.imgur.com/ZFMaW5h.png'
[错误信息2]
ubuntunaoki@LAPTOP-7Q7QL2PR:~$ python3 betsuaka_test.py
Traceback (most recent call last):
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions
yield
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 233, in handle_request
resp = self._pool.handle_request(req)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request
raise exc from None
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request
response = connection.handle_request(
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 101, in handle_request
return self._connection.handle_request(request)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 143, in handle_request
raise exc
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 93, in handle_request
self._send_request_headers(**kwargs)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 151, in _send_request_headers
with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.LocalProtocolError: Illegal header value b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 '
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ubuntunaoki/betsuaka_test.py", line 5, in <module>
client.login(
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/twikit/client.py", line 138, in login
guest_token = self._get_guest_token()
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/twikit/client.py", line 62, in _get_guest_token
response = self.http.post(
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/twikit/http.py", line 54, in post
return self.request('POST', url, **kwargs)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/twikit/http.py", line 25, in request
response = self.client.request(method, url, **kwargs)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_client.py", line 827, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_client.py", line 914, in send
response = self._send_handling_auth(
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_client.py", line 942, in _send_handling_auth
response = self._send_handling_redirects(
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_client.py", line 979, in _send_handling_redirects
response = self._send_single_request(request)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_client.py", line 1015, in _send_single_request
response = transport.handle_request(request)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 232, in handle_request
with map_httpcore_exceptions():
File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "/home/ubuntunaoki/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.LocalProtocolError: Illegal header value b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 '
为什么会出现这种情况?
https://i.imgur.com/ZFMaW5h.png
是有效的,当我在浏览器地址栏上写下这个网址时,就会弹出我想要的图片。从twikit文档我们可以理解
upload_media()
签名:
Uploads media to twitter.
Parameters:
media_path (str | bytes) – The file path or binary data of the media to be uploaded.
index (int) – The index of the media segment being uploaded. Should start from 0 and increment by 1 for each subsequent upload.
Returns:
The media ID of the uploaded media.
Return type:
int
因此您应该传递到本地文件或该文件的字节的
media_path
路径。
我建议您首先从源获取图像,将结果转换为字节,然后将此二进制数据传递给方法
upload_media()
我想我的回答可以帮助你实现你的目标