0

以前質問をさせて頂いたPhantomJSをGoogle Colab上で用いて、データスクレイピングの練習をしています。
が、データをcsvファイル内にうまく格納できず下の様なエラーが出てしまいます...。
どの様に対応すれば良いでしょうか...?

コード

!pip install selenium 

# PhantomJS をダウンロード
!wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
!tar xf phantomjs-2.1.1-linux-x86_64.tar.bz2
#https://review-of-my-life.blogspot.com/2017/10/python-web-scraping-data-collection-analysis.htmlの練習
#trendAnalytics.py
from selenium import webdriver  
from pandas import * 
import time

#Access to page
# PATH を指定した上で WebDriver を用意
browser = webdriver.PhantomJS(executable_path="/content/phantomjs-2.1.1-linux-x86_64/bin/phantomjs") #PhantomJSのサポートは終わっているらしい...?Headless Chromeを使うべきなのか...?
# DO NOT FORGET to set path
url = "http://b.hatena.ne.jp/search/text?safe=on&q=Python&users=50"
browser.get(url)
!touch trend.csv
df = pandas.read_csv('trend.csv',index_cols=0) #下のエラーの要因

エラー

 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
---------------------------------------------------------------------------
EmptyDataError                            Traceback (most recent call last)
<ipython-input-4-f17876ff531c> in <module>()
     11 browser.get(url)
     12 get_ipython().system('touch trend.csv')
---> 13 df = pandas.read_csv('trend.csv') #下のエラーの要因
     14 
     15 #Insert title,date,bookmarks into CSV file

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    707                     skip_blank_lines=skip_blank_lines)
    708 
--> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    447 
    448     # Create the parser.
--> 449     parser = TextFileReader(filepath_or_buffer, **kwds)
    450 
    451     if chunksize or iterator:

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    816             self.options['has_index_names'] = kwds['has_index_names']
    817 
--> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1047     def _make_engine(self, engine='c'):
   1048         if engine == 'c':
-> 1049             self._engine = CParserWrapper(self.f, **self.options)
   1050         else:
   1051             if engine == 'python':

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1693         kwds['allow_leading_cols'] = self.index_col is not False
   1694 
-> 1695         self._reader = parsers.TextReader(src, **kwds)
   1696 
   1697         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

EmptyDataError: No columns to parse from file
0

1 件の回答 1

0

エラーメッセージに出ている通り、CSV として読み込もうとしているファイルが完全に空ファイルで読み込めないためエラーが出ています。この部分は CSV にデータを格納しようとしているのではなく、これからデータを作ろうとしている前段階において pandas DataFrame を用意しようとしています。

引用元のコードがどういう意図で df = pandas.read_csv('trend.csv',index_cols=0) と書いたかは分かりませんが、単に pandas DataFrame を初期化したいのであれば下のように書けばよいです。

df = pandas.DataFrame()

(もしかしたら引用元のコードでは trend.csv というファイル名を使いまわしており、前回取得したデータに新しいデータを付け加えて保存したくてこのようなコードになったのかもしれません。)

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。