首页 > 解决方案 > 我下面的代码可以正常工作并在 python 中打印......但是当我尝试发布它时,它会给出这些错误。你能帮我解决这个问题吗?

问题描述

代码 :

 import requests
 from bs4 import BeautifulSoup      
 import tweepy
 import apikey
      
 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")


     score_check = 0
     init = 0

     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() == 'live':
                 statusRed += 1
         if statusRed == 1:
             teams_parent = i.find("div", class_="teams")
             score = []
             team_names = []
             i=0
             check_one = ''
             check_two = ''
             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 i == 0:
                     score.append(score_info.get_text())
                     check_one = 'Printed A Score'
                     flag_one = 1
                 elif i == 0 and flag_one == 0 :
                     check_one = 'Did Not Print A Score'
                 if score_info and i == 1:
                     score.append(score_info.get_text())
                     check_two = 'Printed B Score'
                     flag_two = 1
                 elif i == 0 and flag_two == 0 :
                     check_two = 'Did Not Print B Score'
                 team_Detail = team.find("div", class_="name-detail")
                 if team_Detail:
                     team_names.append(team_Detail.get_text())
                     i+=1
             #print(check_one)
             #print(check_two)
             if len(team_names) == 2 and len(score) == 2:
                api.update_status(team_names[0], score[0],  'Vs', team_names[1], score[1])
             elif len(team_names) == 2 and check_one == 'Printed A Score':
                 api.update_status(team_names[0], score[0],  'Vs', team_names[1])
             elif len(team_names) == 2 and check_two == 'Printed B Score':
                 api.update_status(team_names[0],  'Vs', team_names[1], score[0])
             else:
                 print('Multiple teams Found For A Match')
             if(score[0] == score[0] + 2 or score[1] == score[1] + 2):
                 init = init + 2



 while(1):
 work()

**问题 :

错误图片:

在此处输入图像描述

它在python中正常工作。但是,当我去发推文时,它会显示这些错误……但是当我简单地打印实时比分时,它就可以了。

这个你能帮我吗。

标签: pythontweepy

解决方案


import requests
from bs4 import BeautifulSoup      
import tweepy
import apikey


#Connects to twitter, check the credentials and returns the api
def connect_to_twitter():
    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")

    finally:
        return api

#Post To Twitter refer to last line of code.
#Add a forloop to post muliple games.
def post_to_twitter(game):
    API = connect_to_twitter()
    return API.update_status(game.get("team_1")+": "+game.get("team_1_score")+" VS "+game.get("team_2")+": "+game.get("team_2_score"))


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

    for game in games:

        score_info = []
        team_info = []
        teams = game.find("div", class_="teams")

        try:
            get_status = game.find("div", class_="status red")
            status = get_status.text


        except AttributeError:
            status = "status error"
            continue

        for team in teams:
            raw_team = team.find("div", class_="name-detail").text
            team_info.append(raw_team)

            try:
                raw_score = team.find("div", class_="score-detail").text
                score_info.append(raw_score)

            except AttributeError:
                score_info.append('0')

        game_dict = {
            "team_1": team_info[0],
            "team_1_score": score_info[0],
            "team_2": team_info[1],
            "team_2_score": score_info[1],
            "status": status
        }

        games_list.append(game_dict)
    return games_list


post_to_twitter(game=scraper()[0])

推荐阅读