【问题标题】:Twint output list is returning 'NoneType' ErrorTwint 输出列表返回 \'NoneType\' 错误
【发布时间】:2023-02-07 11:22:18
【问题描述】:

我正在编写一个简单的程序,将给定的 Twitter 用户的推文逐字保存到 .csv 文件中,并使用 nltk 用词性标记它们。

尝试遍历 twint.output.tweets_list 时,我收到以下错误:

twint.get:User:'NoneType' object is not subscriptable

我知道有推文要返回的事实,所以它不仅仅是缺少推文。

我的代码如下:

import twint
import csv
import nltk

# Configure Twint object

c = twint.Config()
c.Username = "POTUS"
c.Limit = 100

# Run Twint

twint.run.Search(c)

# Open a CSV file and write the tweets and their parts of speech to it

with open('tweets_with_POS.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)
    writer.writerow(["word", "part_of_speech"])
    for tweet in twint.output.tweets_list:
        words = nltk.word_tokenize(tweet.tweet)
        pos_tags = nltk.pos_tag(words)
        for word, pos in pos_tags:
            writer.writerow([word, pos])

我试过从各种网络运行代码,认为它可能是一个 IP 块,但它似乎不是。任何帮助表示赞赏。

如果要重现此代码,则需要包含以下代码 nltk.download('punkt') nltk.download('averaged_perceptron_tagger')

【问题讨论】:

  • “我知道一个事实”- 你验证过twint.output的内容了吗?
  • print(twint.output) 返回完全相同的错误
  • 从我从 Twint 的 Github 上收集到的信息来看,输出只能存储在 .json 文件中。尝试将其输出到 .json 并打开它。

标签: python web-scraping twitter twint


【解决方案1】:

事实证明,问题出在与 Twint 的兼容性问题上。

我运行pip install --upgrade -e git+https://github.com/twintproject/twint.git@origin/master#egg=twint 来升级 twint,由于某种原因它在主分支后面自动下载了一个版本。

然后我遇到了AttributeError: module 'typing' has no attribute '_ClassVar',我通过运行pip uninstall dataclasses -y解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2013-07-23
    相关资源
    最近更新 更多