【问题标题】:Python twint library is not working in Colab environmentPython twint 库在 Colab 环境中不起作用
【发布时间】:2021-07-16 23:11:05
【问题描述】:

我正在尝试在 Colab 中使用 Python 的 twint 库 (Twitter scraper) 运行代码。

我的代码是:

!pip install twint
!pip install nest_asyncio
!pip install pandas

import twint
import nest_asyncio
nest_asyncio.apply()
import time
import pandas as pd
import os
import re

timestr = time.strftime("%Y%m%d")

c = twint.Config()
c.Limit = 1000
c.Lang = "en"
c.Store_csv = True
c.Search = "apple"
c.Output = timestr + "_en_apple.csv"
twint.run.Search(c)

上面的代码在我机器上的 Jupyter 中完美运行并获取推文。但是,Colab 中的相同代码会导致以下结果:

 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 1.0 secs
 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 8.0 secs
 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 27.0 secs
 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 64.0 secs
 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 125.0 secs
 CRITICAL:root:twint.run:Twint:Feed:noDataExpecting value: line 1 column 1 (char 0)
 sleeping for 216.0 secs

如何在 Colab 中解决此问题?

【问题讨论】:

    标签: python-3.x web-scraping google-colaboratory twint


    【解决方案1】:

    我得到了以下在 Google Colab 中的工作。从 requirements.txt 安装更简单。

    !git clone --depth=1 https://github.com/twintproject/twint.git
    !cd /content/twint && pip3 install . -r requirements.txt
    import twint
    import nest_asyncio
    nest_asyncio.apply()
    import time
    import pandas as pd
    import os
    import re
    timestr = time.strftime("%Y%m%d")
    
    c = twint.Config()
    c.Limit = 1000
    c.Lang = "en"
    c.Store_csv = True
    c.Search = "apple"
    c.Output = timestr + "_en_apple.csv"
    twint.run.Search(c)
    

    【讨论】:

    • 我试过了,它给了我错误:AttributeError: module 'twint' has no attribute 'Config'
    • 您是否创建了一个全新的 Colab 笔记本并将代码粘贴到单个单元格中并运行它?
    • 这是我的 Colab 笔记本的链接。 colab.research.google.com/drive/…
    • 工作,将所有内容放入一个单元格后。我只是好奇,为什么它不能在不同的单元格中工作。
    【解决方案2】:

    对于那些构建失败的人,像这样编辑你的 requirements.txt;

    aiohttp==3.7.0
    aiogram==2.2
    aiodns
    beautifulsoup4
    cchardet
    dataclasses
    elasticsearch
    pysocks
    pandas>=0.23.0
    aiohttp_socks<=0.4.1
    schedule
    geopy
    fake-useragent
    googletransx
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2015-05-13
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多