python 尝试构建Twitter新闻机器人

3yhwsihp  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(120)

我试图建立一个基本的twitter新闻机器人,我想知道为什么这段代码不会运行:

import tweepy
import feedparser

# Set up Twitter API keys and access tokens
consumer_key = "YOUR_CONSUMER_KEY"
consumer_secret = "YOUR_CONSUMER_SECRET"
access_token = "YOUR_ACCESS_TOKEN"
access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"

# Authenticate with Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# Define function to post tweets
def post_tweet(title, url):
    tweet = f"{title}\n{url}"
    api.update_status(tweet)

# Get Bitcoin-related news from Coindesk RSS feed
rss_url = "https://www.coindesk.com/feed"
feed = feedparser.parse(rss_url)

# Filter news articles related to Bitcoin
bitcoin_articles = [entry for entry in feed.entries if "bitcoin" in entry.title.lower()]

# Post the selected articles to Twitter
for article in bitcoin_articles:
    post_tweet(article.title, article.link)

我已经添加了正确的密钥和令牌。终端没有错误,什么都没有发生。
我尝试重新安装依赖项并重新确认密钥和令牌是正确的,但仍然一无所获。

6kkfgxo0

6kkfgxo01#

您正在使用重定向到其他URL的URL(解析器无法处理重定向)。以下代码打印:

import feedparser

# Get Bitcoin-related news from Coindesk RSS feed
rss_url = "https://www.coindesk.com/arc/outboundfeeds/rss/"
feed = feedparser.parse(rss_url)

bitcoin_articles = [entry for entry in feed.entries if 'bitcoin' in entry.title.lower()]

# Post the selected articles to Twitter
for article in bitcoin_articles:
    print(article)

输出(只有2个条目,因为太多):

{'title': 'Bitcoin Holds Near $29K as Investors Weigh Rate Increases, Banking Contagion', 'title_detail': {'type': 'text/plain', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': 'Bitcoin Holds Near $29K as Investors Weigh Rate Increases, Banking Contagion'}, 'links': [{'rel': 'alternate', 'type': 'text/html', 'href': 'https://www.coindesk.com/markets/2023/05/04/bitcoin-holds-near-29k-as-investors-weigh-rate-increases-banking-contagion/?utm_medium=referral&utm_source=rss&utm_campaign=headlines'}], 'link': 'https://www.coindesk.com/markets/2023/05/04/bitcoin-holds-near-29k-as-investors-weigh-rate-increases-banking-contagion/?utm_medium=referral&utm_source=rss&utm_campaign=headlines', 'id': 'NRJZDVTWFBEQHAV2SDTAZHCW7A', 'guidislink': False, 'authors': [{'name': 'Jocelyn Yang'}], 'author': 'Jocelyn Yang', 'author_detail': {'name': 'Jocelyn Yang'}, 'summary': 'BTC traded flat after a slight dip early Thursday. Ether also traded in a narrow range.', 'summary_detail': {'type': 'text/html', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': 'BTC traded flat after a slight dip early Thursday. Ether also traded in a narrow range.'}, 'published': 'Thu, 04 May 2023 21:52:25 +0000', 'published_parsed': time.struct_time(tm_year=2023, tm_mon=5, tm_mday=4, tm_hour=21, tm_min=52, tm_sec=25, tm_wday=3, tm_yday=124, tm_isdst=0), 'updated': '2023-05-04T21:52:26.131+00:00', 'updated_parsed': time.struct_time(tm_year=2023, tm_mon=5, tm_mday=4, tm_hour=21, tm_min=52, tm_sec=26, tm_wday=3, tm_yday=124, tm_isdst=0), 'tags': [{'term': 'Markets', 'scheme': 'https://www.coindesk.com/markets/', 'label': None}, {'term': 'news', 'scheme': 'article-type', 'label': None}, {'term': 'Bitcoin', 'scheme': 'tag', 'label': None}, {'term': 'Ether', 'scheme': 'tag', 'label': None}, {'term': 'banks', 'scheme': 'tag', 'label': None}, {'term': 'Markets', 'scheme': 'tag', 'label': None}, {'term': 'Trading', 'scheme': 'tag', 'label': None}, {'term': 'Investing', 'scheme': 'tag', 'label': None}], 'media_keywords': '', 'media_content': [{'url': 'https://www.coindesk.com/resizer/M1840gmUQGHm9RhnTjMHG3lo-1k=/800x600/cloudfront-us-east-1.images.arcpublishing.com/coindesk/KDMHTAPNOVFGZILKV6Z4DUGTJU.jpg', 'type': 'image/jpeg', 'height': '600', 'width': '800'}], 'content': [{'type': 'text/plain', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': '(Getty Images)'}], 'media_credit': [{'role': 'author', 'scheme': 'urn:ebu', 'content': 'Mario Tama'}], 'credit': 'Mario Tama'}
{'title': 'Bitcoin’s Frenzy of Activity Pushes Average Transaction Fee Over $7, Nearly 2-Year High', 'title_detail': {'type': 'text/plain', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': 'Bitcoin’s Frenzy of Activity Pushes Average Transaction Fee Over $7, Nearly 2-Year High'}, 'links': [{'rel': 'alternate', 'type': 'text/html', 'href': 'https://www.coindesk.com/tech/2023/05/04/bitcoins-frenzy-of-activity-pushes-average-transaction-fee-over-7-nearly-2-year-high/?utm_medium=referral&utm_source=rss&utm_campaign=headlines'}], 'link': 'https://www.coindesk.com/tech/2023/05/04/bitcoins-frenzy-of-activity-pushes-average-transaction-fee-over-7-nearly-2-year-high/?utm_medium=referral&utm_source=rss&utm_campaign=headlines', 'id': 'C2KJV5E5IJHXZGXLAWCLHVOD2U', 'guidislink': False, 'authors': [{'name': 'Sage D. Young'}], 'author': 'Sage D. Young', 'author_detail': {'name': 'Sage D. Young'}, 'summary': 'The average transaction fee on Bitcoin has shot above $7, pushing total fees up nearly five-fold in two weeks– thanks to a surge in Ethereum-style “BRC-20” tokens and NFT-like “inscriptions” on the increasingly popular Ordinals project.', 'summary_detail': {'type': 'text/html', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': 'The average transaction fee on Bitcoin has shot above $7, pushing total fees up nearly five-fold in two weeks– thanks to a surge in Ethereum-style “BRC-20” tokens and NFT-like “inscriptions” on the increasingly popular Ordinals project.'}, 'published': 'Thu, 04 May 2023 21:30:58 +0000', 'published_parsed': time.struct_time(tm_year=2023, tm_mon=5, tm_mday=4, tm_hour=21, tm_min=30, tm_sec=58, tm_wday=3, tm_yday=124, tm_isdst=0), 'updated': '2023-05-04T21:37:08.234+00:00', 'updated_parsed': time.struct_time(tm_year=2023, tm_mon=5, tm_mday=4, tm_hour=21, tm_min=37, tm_sec=8, tm_wday=3, tm_yday=124, tm_isdst=0), 'tags': [{'term': 'Technology', 'scheme': 'https://www.coindesk.com/tech/', 'label': None}, {'term': 'news', 'scheme': 'article-type', 'label': None}, {'term': 'Brc 20', 'scheme': 'tag', 'label': None}, {'term': 'Bitcoin', 'scheme': 'tag', 'label': None}, {'term': 'NFT', 'scheme': 'tag', 'label': None}, {'term': 'Ordinals', 'scheme': 'tag', 'label': None}, {'term': 'Miners', 'scheme': 'tag', 'label': None}, {'term': 'Fees', 'scheme': 'tag', 'label': None}], 'media_keywords': '', 'media_content': [{'url': 'https://www.coindesk.com/resizer/3C3vXFHpyv0AW6mh6qh-x0HFOhc=/800x600/cloudfront-us-east-1.images.arcpublishing.com/coindesk/KMQAI6XKSFA6FODI5DOCB4FXUY.jpg', 'type': 'image/jpeg', 'height': '600', 'width': '800'}], 'content': [{'type': 'text/plain', 'language': None, 'base': 'https://www.coindesk.com/arc/outboundfeeds/rss/', 'value': '(Glassnode)'}]}

相关问题