You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku日志显示旧代码报错未加载新部署代码是什么原因

Heroku运行旧版本代码导致爬虫报错排查

报错背景

Heroku定时任务执行python send_email.py时抛出索引越界错误,错误日志显示运行的是旧版O Globo头条提取逻辑,使用的XPath路径为//h1[@class="teaser__title headline__title"]//a/@href,对应代码位置为scraper_portal.py第50行。本地已将提取逻辑更新为XPath//h1[@class="title"]/parent::a/@href,但线上环境未加载该更新。
完整错误日志如下:

2022-06-08T17:20:08.303086+00:00 heroku[scheduler.9867]: Starting process with command `python send_email.py`
2022-06-08T17:20:08.971064+00:00 heroku[scheduler.9867]: State changed from starting to up
2022-06-08T17:20:15.335273+00:00 app[scheduler.9867]: Traceback (most recent call last):
2022-06-08T17:20:15.335302+00:00 app[scheduler.9867]:   File "/app/send_email.py", line 15, in <module>
2022-06-08T17:20:15.335398+00:00 app[scheduler.9867]:     manchete_oglobo = link_oglobo()
2022-06-08T17:20:15.335400+00:00 app[scheduler.9867]:   File "/app/scraper_portal.py", line 50, in link_oglobo
2022-06-08T17:20:15.335471+00:00 app[scheduler.9867]:     manchete_oglobo = doc.xpath('//h1[@class="teaser__title headline__title"]//a/@href')[0]
2022-06-08T17:20:15.335490+00:00 app[scheduler.9867]: IndexError: list index out of range
2022-06-08T17:20:15.471090+00:00 heroku[scheduler.9867]: Process exited with status 1
2022-06-08T17:20:15.530981+00:00 heroku[scheduler.9867]: State changed from up to complete

本地最新代码逻辑:

import requests
import lxml.html

def link_oglobo():
  url = 'https://oglobo.globo.com/'
  page = requests.get(url)
  doc = lxml.html.fromstring(page.content)
  manchete_oglobo = doc.xpath('//h1[@class="title"]/parent::a/@href')[0]
  return manchete_oglobo

核心原因及排查方向

  • 未完成完整部署流程:本地代码修改后如果仅保存文件、没有依次执行git add .、git commit -m "更新O Globo头条提取XPath"、git push heroku <部署分支名>的推送流程,Heroku端不会自动同步本地未提交的代码,会一直运行最后一次成功推送的版本。
  • 部署分支不匹配:Heroku默认部署main分支的代码,如果本地更新提交在其他分支(如dev、feature分支),直接执行git push heroku不会将非默认分支代码部署到线上,需要显式执行分支映射推送,例如git push heroku dev:main将本地dev分支部署到线上主环境。
  • 部署失败自动回滚:执行heroku releases可查看所有历史部署记录,如果最新一次部署状态不是succeeded,说明构建过程中出现依赖安装失败、语法错误等问题,Heroku会自动回滚到上一个可正常运行的版本,新提交的代码不会生效。
  • 构建缓存未刷新:极少数情况会出现旧Slug缓存未清理的问题,先执行heroku plugins:install heroku-repo安装仓库管理插件,再运行heroku repo:purge_cache -a <你的Heroku应用名>清除构建缓存,重新推送代码部署即可加载最新版本。

验证方式

部署完成后执行heroku run bash进入线上运行容器,直接查看/app/scraper_portal.py文件内容,确认对应XPath逻辑和本地最新代码一致后,手动触发定时任务验证运行结果即可。

内容的提问来源于stack exchange,提问作者polo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.02 05:49:19