以 16 为底数的 int() 的无效文字:b''
invalid literal for int() with base 16: b''
背景 - 我正在尝试创建一个 Twitter 机器人,它实时侦听仅来自某些帐户的某些关键字。当满足这些条件时,我会用我的应用程序转发它,这样我就可以跟进来自我的主帐户的通知。
问题 - 有时,但并非总是如此,当脚本为 运行 时,我会遇到错误,这会给我在本 [= 的主题中列出的错误37=]。我不确定是什么原因造成的,因为它是间歇性发生的。
我做了什么 - 我已经搜索了错误;我发现的大部分内容都是指“base 10”,而不是“base 16”。对于我发现它引用 base 16 的几个案例,我对解决方案的理解不够好,无法使其适应我的代码(在这个项目上自学)。
代码
import tweepy
import json
import re
import logging
auth = tweepy.OAuthHandler("xxxx", "xxxx")
auth.set_access_token("yyyyy", "yyyy")
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
self.me = api.me()
def on_status(self, tweet):
keyword = ["Keyword1", "Keyword2"]
accounts = ['account1','account2']
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in keyword]
patterns2 = [r'\b%s\b' % re.escape(s.strip()) for s in accounts]
there = re.compile('|'.join(patterns))
there2 = re.compile('|'.join(patterns2))
if there.search(tweet.text) and there2.search(str(tweet.user.id)):
print("New")
tweet.retweet()
def on_error(self, status):
print("Error detected")
tweets_listener = MyStreamListener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
stream.filter(follow=['account1','account2'])
错误
ValueError Traceback (most recent call last)
~\anaconda3\lib\http\client.py in _get_chunk_left(self)
554 try:
555 chunk_left = self._read_next_chunk_size()
556 except ValueError:
~\anaconda3\lib\http\client.py in _read_next_chunk_size(self)
521 try:
522 return int(line, 16)
523 except ValueError:
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
IncompleteRead Traceback (most recent call last)
然后我得到了一些不完整的读数,错误的最后一行是:
ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
这现在应该在主分支上的 Tweepy 开发版本中通过自动尝试重新连接来处理。底层连接错误可能是由于在处理推文时在流中落后太多。目前可以尝试减少on_status
内的处理,通过重启流处理错误,等待Tweepy v4.0,或者使用Tweepy的开发版本。
有关更多上下文,请参阅 https://github.com/tweepy/tweepy/issues/237 and https://github.com/tweepy/tweepy/issues/448。
背景 - 我正在尝试创建一个 Twitter 机器人,它实时侦听仅来自某些帐户的某些关键字。当满足这些条件时,我会用我的应用程序转发它,这样我就可以跟进来自我的主帐户的通知。
问题 - 有时,但并非总是如此,当脚本为 运行 时,我会遇到错误,这会给我在本 [= 的主题中列出的错误37=]。我不确定是什么原因造成的,因为它是间歇性发生的。
我做了什么 - 我已经搜索了错误;我发现的大部分内容都是指“base 10”,而不是“base 16”。对于我发现它引用 base 16 的几个案例,我对解决方案的理解不够好,无法使其适应我的代码(在这个项目上自学)。
代码
import tweepy
import json
import re
import logging
auth = tweepy.OAuthHandler("xxxx", "xxxx")
auth.set_access_token("yyyyy", "yyyy")
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
self.me = api.me()
def on_status(self, tweet):
keyword = ["Keyword1", "Keyword2"]
accounts = ['account1','account2']
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in keyword]
patterns2 = [r'\b%s\b' % re.escape(s.strip()) for s in accounts]
there = re.compile('|'.join(patterns))
there2 = re.compile('|'.join(patterns2))
if there.search(tweet.text) and there2.search(str(tweet.user.id)):
print("New")
tweet.retweet()
def on_error(self, status):
print("Error detected")
tweets_listener = MyStreamListener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
stream.filter(follow=['account1','account2'])
错误
ValueError Traceback (most recent call last)
~\anaconda3\lib\http\client.py in _get_chunk_left(self)
554 try:
555 chunk_left = self._read_next_chunk_size()
556 except ValueError:
~\anaconda3\lib\http\client.py in _read_next_chunk_size(self)
521 try:
522 return int(line, 16)
523 except ValueError:
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
IncompleteRead Traceback (most recent call last)
然后我得到了一些不完整的读数,错误的最后一行是:
ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
这现在应该在主分支上的 Tweepy 开发版本中通过自动尝试重新连接来处理。底层连接错误可能是由于在处理推文时在流中落后太多。目前可以尝试减少on_status
内的处理,通过重启流处理错误,等待Tweepy v4.0,或者使用Tweepy的开发版本。
有关更多上下文,请参阅 https://github.com/tweepy/tweepy/issues/237 and https://github.com/tweepy/tweepy/issues/448。