Python Twitter Bot本地正常但Heroku部署崩溃的排查修复咨询
错误原因
- 文件编码不匹配:本地生成的
quotes3.json采用Windows系统默认编码(如Windows-1252),而Heroku的Linux环境默认以UTF-8编码读取文件,导致解码时遇到无法识别的字节(0x93),抛出UnicodeDecodeError。 - 依赖本地预生成文件:代码未在Heroku运行时动态生成JSON文件,而是直接读取本地提交的编码不兼容的文件。
修复方案
方案1:强制指定文件编码读取(快速修复)
修改get_quote()函数中读取JSON文件的代码,明确指定与本地文件匹配的编码:
def get_quote(): # 指定编码为Windows-1252,匹配本地生成文件的编码 with open('quotes3.json', 'r', encoding='windows-1252') as f: read_f = json.load(f) # 后续代码保持不变 random_quote = random.choice(read_f) author = random_quote['author'] quote_text = f"{random_quote['quote']}" tweet = re.sub("[^A-Za-z0-9.,:;' ]","",quote_text) ftweet= tweet +" - " +author +" #Inspiration_of_the_day" while True: if len(ftweet) <=280: return ftweet
方案2:在Heroku上动态生成JSON文件(推荐,彻底解决编码问题)
避免依赖本地文件,让应用在Heroku启动时自动爬取生成UTF-8编码的JSON文件:
- 修改
quotes_scraper()函数,写文件时明确指定UTF-8编码:
def quotes_scraper(): collection = list() TAGS = ["science","humor","life", "knowledge","motivational", "inspirational", "truth", "philosophy" ] for tag in TAGS: url = f"https://www.goodreads.com/quotes/tag/{tag}" page = requests.get(url) # 改用page.text让requests自动处理响应编码 soup = BeautifulSoup(page.text, "html.parser") raw_data = soup.find_all(class_ ="quoteText") for quote in raw_data: quote_text = quote.contents[0].strip() author = quote.find(class_="authorOrTitle").text.strip() data = { "quote": quote_text, "author": author, "genre": tag } collection.append(data) # 明确指定UTF-8编码写入文件 with open("quotes3.json", "w", encoding='utf-8') as qfile: json.dump(collection, qfile, ensure_ascii=False) return collection
- 修改
run()函数,启动时先调用quotes_scraper()生成文件:
def run(): # 启动时先爬取生成UTF-8编码的quotes3.json quotes_scraper() while True: tweet() time.sleep(10) like_and_retweet() time.sleep(run_tm(60*60*4,60*60*10))
- 将
quotes3.json加入.gitignore文件,避免提交本地编码的文件到Heroku:
# .gitignore内容 quotes3.json
额外优化(可选)
添加异常处理,避免爬取失败导致应用崩溃:
def quotes_scraper(): collection = list() TAGS = ["science","humor","life", "knowledge","motivational", "inspirational", "truth", "philosophy" ] for tag in TAGS: url = f"https://www.goodreads.com/quotes/tag/{tag}" try: page = requests.get(url, timeout=10) page.raise_for_status() # 抛出HTTP错误 soup = BeautifulSoup(page.text, "html.parser") raw_data = soup.find_all(class_ ="quoteText") for quote in raw_data: quote_text = quote.contents[0].strip() author = quote.find(class_="authorOrTitle").text.strip() data = { "quote": quote_text, "author": author, "genre": tag } collection.append(data) except Exception as e: print(f"爬取标签 {tag} 失败: {e}") continue with open("quotes3.json", "w", encoding='utf-8') as qfile: json.dump(collection, qfile, ensure_ascii=False) return collection
内容的提问来源于stack exchange,提问作者kana natasha
相关产品推荐
相关产品推荐

