python - 我下面的代码可以正常工作并在 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中正常工作。但是,当我去发推文时,它会显示这些错误……但是当我简单地打印实时比分时,它就可以了。
这个你能帮我吗。
解决方案
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])
推荐阅读
- django - Django:创建查询集并向其中添加数据
- django - 如何通过模板中的外键值过滤数据
- java - 如何使用数组自动生成 ID?以及如何为每个货运订单创建货运状态?
- java - 如何在 Spring Boot 中从旧令牌生成新的 JWT 令牌?
- javascript - 如何从 Angular/Typescript 中的外部 javascript 文件更新变量
- jquery - 为每个其他项目添加一个类
- typescript - 具有默认导出功能的 Typescript 环境声明
- python - 尝试使用 x-api-key 在站点上进行授权时出现“ValueError:转换说明符后预期的':'”
- postgresql - SonarQube:如何连接到启用 SSL 的 Google Cloud Postgresql 服务器
- android - 如何在Android studio中实现fragments中的按钮功能