如何解决此 Python Snscrape 错误

问题描述 投票:0回答:1

为什么我在 snscrape 上收到此错误?我已经实时更新了python和snscrape,都更新了。

PS C:\Users\muzam\OneDrive\Desktop\Summer\project 1> python -u "c:\Users\muzam\OneDrive\Desktop\Summer\project 1\Daraz Sentiment Analysis\tempCodeRunnerFile.py"
Traceback (most recent call last):
  File "c:\Users\muzam\OneDrive\Desktop\Summer\project 1\Daraz Sentiment Analysis\tempCodeRunnerFile.py", line 1, in <module>
    import snscrape.modules.twitter as sntwitter
  File "C:\Users\muzam\AppData\Local\Programs\Python\Python312\Lib\site-packages\snscrape\modules\__init__.py", line 17, in <module>       
    _import_modules()
  File "C:\Users\muzam\AppData\Local\Programs\Python\Python312\Lib\site-packages\snscrape\modules\__init__.py", line 13, in _import_modules
    module = importer.find_module(moduleName).load_module(moduleName)
             ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'FileFinder' object has no attribute 'find_module'`

我的此错误代码:

import snscrape.modules.twitter as sntwitter
import csv
def is_english_textual_tweet(tweet):
    return (
        tweet.lang == 'en' and
        not tweet.retweetedTweet and
        not tweet.inReplyToTweetId and
        not tweet.media
    )
username = 'nfak_ism_'
tweets = []
for tweet in sntwitter.TwitterSearchScraper(f'from:{username}').get_items():
    if is_english_textual_tweet(tweet):
        tweets.append({
            'date': tweet.date.strftime('%Y-%m-%d %H:%M:%S'),
            'content': tweet.content,
            'id': tweet.id,
            'username': tweet.user.username
        })
csv_file = 'tweets.csv'
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
    writer = csv.DictWriter(file, fieldnames=['date', 'content', 'id', 'username'])
    writer.writeheader()
    writer.writerows(tweets)

print(f'Scraped {len(tweets)} tweets from @{username}')
import pandas as pd
df = pd.read_csv(csv_file)
print(df.head(3))

python twitter screen-scraping
1个回答
0
投票

问题似乎是您对 Python 3.12 的使用。

根据 PyPI,snscrape 仅在 Python 3.8 至 3.11 上受支持。尝试降级到 Python 3.11。

如果我们仔细查看您的回溯,您的代码会在第一行遇到异常,

import snscrape.modules.twitter as sntwitter
。事实上,我发现我得到了与您仅仅尝试在 Python 3.12 中导入包相同的错误:

Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import snscrape.modules.twitter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Luke\.virtualenvs\78685974-0LPcIQMG\Lib\site-packages\snscrape\modules\__init__.py", line 17, in <module>
    _import_modules()
  File "C:\Users\Luke\.virtualenvs\78685974-0LPcIQMG\Lib\site-packages\snscrape\modules\__init__.py", line 13, in _import_modules
    module = importer.find_module(moduleName).load_module(moduleName)
             ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'FileFinder' object has no attribute 'find_module'

另一方面,使用 Python 3.11,包导入得很好:

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import snscrape.modules.twitter
>>>
© www.soinside.com 2019 - 2024. All rights reserved.