python - 不断收到这个 AttributeError:部分初始化的模块 'schedule' 没有属性 'every'
问题描述
有人能帮我解决这个错误吗?它基本上从https://www.worldometers.info/coronavirus/抓取数据并在 Twitter 上发布更新。
我遇到的问题与第 5 行和第 24 行的调度程序有关。
回溯(最近一次通话最后):
文件“C:\Users\Abdul\Documents\CoronavirusBot\twitter_bot.py”,第 5 行,在
进口时间表
文件“C:\Users\Abdul\Documents\CoronavirusBot\schedule.py”,第 24 行,在
schedule.every().day.at("19:51").do(submit_tweet)
AttributeError:部分初始化的模块 'schedule' 没有属性 'every'(很可能是由于循环导入)
Twitter_bot.py
from config import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
import tweepy
import requests
import schedule
import time
from lxml import html
def create_tweet():
response = requests.get('https://www.worldometers.info/coronavirus/')
doc = html.fromstring(response.content)
total, deaths, recovered = doc.xpath('//div[@class="maincounter-number"]/span/text()')
tweet = f'''Coronavirus Latest Updates
Total cases: {total}
Recovered: {recovered}
Deaths: {deaths}
Source: https://www.worldometers.info/coronavirus/
#coronavirus #covid19 #coronavirusnews #coronavirusupdates
'''
return tweet
if __name__ == '__main__':
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Create API object
api = tweepy.API(auth)
try:
api.verify_credentials()
print('Authentication Successful')
except:
print('Error while authenticating API')
sys.exit(1)
while True:
schedule.run_pending()
time.sleep(1)
tweet = create_tweet()
api.update_status(tweet)
print('Tweet successful')
else:
print('error') ```
**Schedule.py**
```import schedule
#define function create tweet
#auth and create tweet
def submit_tweet(*args, **kwargs): #Add the needed args here
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Create API object
api = tweepy.API(auth)
try:
api.verify_credentials()
print('Authentication Successful')
except:
print('Error while authenticating API')
sys.exit(1)
tweet = create_tweet()
api.update_status(tweet)
print('Tweet successful')
schedule.every().day.at("21:19").do(submit_tweet)
while True:
schedule.run_pending()
time.sleep(1)
解决方案
这发生在你的 python 代码文件名是'schedule.py'。如果您将文件名更改为另一个,您将不再看到错误。
推荐阅读
- php - Wordpress Divi 页面编辑
- mysql - 如何将缓冲区数组转换为图像
- reactjs - 如何在 ReactJs 中将参数从父组件传递给子组件?
- react-native - 无法使用 expo 安装 expo-app-loading
- wpf - WPF 我无法在后面的代码中更新组合框值
- vue.js - 当 fetchPolicy 为无缓存时,如何在 vue-apollo 中查看加载状态?
- c# - 使用关键字的语法怪异
- javascript - Uncaught TypeError: parentInstance.children.push is not a function error in react-pdf/renderer in Next.js
- sql - 如何在 SQL 中查询 3 个表?
- java - Java:消息中包含两部分的 WSDL 文件引发异常错误