前提・実現したいこと
私は現在クローリング用のプログラムを参考書を元に作成し
入力:検索したい語句が入ったファイル名、保存したいURLの数、出力するファイル名
出力:語句に対するURLが入力数だけ保存されたファイル、進捗状況を保存したファイル(search_progress.txt)
というプログラムを作成しました。
それを用いてクローリングを実行したいのですが、端末から動作させた所で下記のエラーを吐いてしまい想定どおりに動きません。
どうすればよろしいでしょうか?
発生している問題・エラーメッセージ
端末に打ち込んだコード:python2 google_search.py UC.csv 10 res.txt
UC.csv の内容:ガンダム,宇宙世紀 のみを書いたCSVファイル
Traceback (most recent call last): File "google_search.py", line 73, in <module> main() File "google_search.py", line 69, in main get_urls_from(keywords,search_num) File "google_search.py", line 49, in get_urls_from progress_file = open('search_progress.txt','a') IOError: [Errno 13] Permission denied: 'search_progress.txt' [urai@localhost crawling]$ python google_search.py UC.csv 10 res.txt /usr/lib/python2.6/site-packages/google.py:203: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. The code that caused this warning is on line 203 of the file /usr/lib/python2.6/site-packages/google.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor. soup = BeautifulSoup(html) Traceback (most recent call last): File "google_search.py", line 73, in <module> main() File "google_search.py", line 69, in main get_urls_from(keywords,search_num) File "google_search.py", line 56, in get_urls_from for url in urls: File "/usr/lib/python2.6/site-packages/google.py", line 204, in search anchors = soup.find(id='search').findAll('a') AttributeError: 'NoneType' object has no attribute 'findAll'
該当のソースコード
python2系での動作を前提
1# -*- coding: utf-8 -*- 2from google import search 3import sys 4import time 5import codecs 6 7 8def make_search_keywords(search_candidates): 9 """ 10 入力は検索したい用語をまとめたファイル 11 「,]区切りされたファイルの一番右の要素のリストがかえってくる 12 """ 13 keyword={} 14 for i,line in enumerate(open(search_candidates,'r')): 15 line = line.rstrip().split(',') 16 while '' in line: 17 line.remove('') 18 """ 19 検索用語を空白区切りにして使いたい場合 20 例:XX大学,XX学部 ==> XX大学 XX学部 21 map_line = map(str,line) 22 keyword[i] = ' '.join(map_line) 23 """ 24 keyword[i] = line[-1] 25 return keyword.values() 26 27 28def write(data,f_name): 29 """ 30 入力は書き込みたいデータとファイル名 31 データを指定されたファイルに書き込む 32 """ 33 f_name.write(data+'\n') 34 35 36def output_progress(keyword,f_name): 37 """ 38 検索済みのキーワードを保存しておいて 39 途中停止が起きても続きから始められるようにしている 40 """ 41 print '現在の検索語:',keyword 42 write(keyword,f_name) 43 44 45def get_urls_from(keywords,search_num): 46 """ 47 入力は検索語のリストと取得するwebサイトのurlの整数 48 """ 49 progress_file = open('search_progress.txt','a') 50 url_file = open(sys.argv[3],'a') 51 for keyword in keywords: 52 write(keyword,url_file) 53 #XXX pauseが短すぎるとgoogleにブロックされシャットダウンするまで使えなくなる. 54 #(20)なら安全だが時間がかかる 55 urls = search(keyword, lang="jp", num=search_num,stop=search_num,pause=20) 56 for url in urls: 57 write(url,url_file) 58 output_progress(keyword,progress_file) 59 time.sleep(20) 60 progress_file.close() 61 url_file.close() 62 63 64 65def main(): 66 search_candidates = sys.argv[1] 67 search_num = int(sys.argv[2]) 68 keywords = make_search_keywords(search_candidates) 69 get_urls_from(keywords,search_num) 70 71 72if __name__ == '__main__': 73 main()
試したこと
何が原因かされわからないので何もできていません。
補足情報(FW/ツールのバージョンなど)
実行環境は
Python 2.6.6
BeautifulSoup (3.2.1)
google (1.6)
lxml (2.2.3)
pykakasi (0.24)
pyOpenSSL (0.13.1)
requests (2.18.4)
になっています。
回答1件
あなたの回答
tips
プレビュー