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

Python用Tweepy发布爬取的板球比分报错,简单文本可正常发布

问题排查及修复方案

核心错误原因

  • tweepy的update_status()方法仅第一个参数接收要发布的推文内容,你将队名、比分等内容作为多个独立参数传入,后续参数会被识别为回复ID、媒体ID等非文本参数,导致参数类型不匹配报错。

其他需修复的问题

  • 代码最后while(1):下的work()缺少缩进,运行会直接触发语法错误
  • 死循环未设置等待间隔,会高频请求目标站点和推特接口,很快会被限流封禁
  • if(score[0] == score[0] + 2 or score[1] == score[1] + 2)逻辑完全无效,score中存储的是字符串无法直接做数值加法,且数值层面自身不可能等于自身加2,该判断永远不成立
  • 爬取的比分、队名字符串可能存在特殊字符、多余换行空格,直接发布可能触发推特内容校验规则

修复后的代码示例

import requests
from bs4 import BeautifulSoup      
import tweepy
import apikey
import time
      
def work():
    auth = tweepy.OAuthHandler(apikey.API_KEY, apikey.API_SECRET_KEY)
    auth.set_access_token(apikey.ACCESS_TOKEN, apikey.ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)

    try:
          api.verify_credentials()
          print("Authentication OK")
    except:
          print("Error during authentication")
          return

    url = "https://www.espncricinfo.com/live-cricket-score"
    r = requests.get(url)
    htmlContent = r.content
    soup = BeautifulSoup(htmlContent, 'html.parser')
    live_check = soup.find_all("div", class_="match-info match-info-FIXTURES")

    api.update_status('----Live Scores----')

    for i in live_check:
        statusRed = 0
        for ik in i.children:
            if ik['class'] == ['status', 'red'] and ik.get_text().strip() == 'live':
                statusRed += 1
        if statusRed == 1:
            teams_parent = i.find("div", class_="teams")
            score = []
            team_names = []
            idx = 0
            flag_one = 0
            flag_two = 0

            teams = teams_parent.find_all("div", class_="team")
            for team in teams:
                score_info = team.find("div", class_="score-detail")
                if score_info and idx == 0:
                    # 清理多余空白字符
                    score.append(score_info.get_text().strip())
                    flag_one = 1
                if score_info and idx == 1:
                    score.append(score_info.get_text().strip())
                    flag_two = 1
                team_Detail = team.find("div", class_="name-detail")
                if team_Detail:
                    team_names.append(team_Detail.get_text().strip())
                    idx += 1
            tweet_content = ""
            if len(team_names) == 2 and len(score) == 2:
                tweet_content = f"{team_names[0]} {score[0]} Vs {team_names[1]} {score[1]}"
            elif len(team_names) == 2 and flag_one == 1:
                tweet_content = f"{team_names[0]} {score[0]} Vs {team_names[1]}"
            elif len(team_names) == 2 and flag_two == 1:
                tweet_content = f"{team_names[0]} Vs {team_names[1]} {score[0]}"
            else:
                print('Multiple teams Found For A Match')
                continue
            # 发布前打印校验内容
            print(f"准备发布:{tweet_content}")
            api.update_status(tweet_content)

# 每5分钟拉取一次数据,可根据需求调整间隔
while True:
    work()
    time.sleep(300)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 04:18:00