###前提・実現したいこと
口コミサイト(googleplace)のクローラです。
定期的に自分で作成した店舗リストにある店舗の口コミを回収して、諸々の分析をするものです。
現在、半分くらいのものはローカル、サーバー問わずエラーが出ないのですが、一部のもののみ、共通点(店舗ID、口コミ内容の文字種類、文字数、)は見られないがエラーがでる。
####環境
Azure
Ubuntu16.04 LTS
python3.6.1,
falcon
###発生している問題・エラーメッセージ
ローカルでのテストは問題なく通るのですが、サーバーにデプロイすると手動で実行しても以下のエラーがでる。
JSONDecodeError('Expecting value: line 1 column 1 (char 0)',) <Response [504]>
実際にエラーがでるデータのうちの一つです。
data: 生データ
data_json: utf-8でエンコードしたデータ
data: {'storeId': '58fb4bdf7392d776ef8ca1e8', 'media': 6, 'content': '何がなんでも個室で、を重視するならば、良い居酒屋といえます。メニューは豊富で、明太子入り卵焼きは想像以上に美味でした。', 'score': {'numeric': 3}, 'poster': {'user': {'name': '原圭助', 'posted': 364}}} data_json: {"storeId": "58fb4bdf7392d776ef8ca1e8", "media": 6, "content": "\u4f55\u304c\u306a\u3093\u3067\u3082\u500b\u5ba4\u3067\u3001\u3092\u91cd\u8996\u3059\u308b\u306a\u3089\u3070\u3001\u826f\u3044\u5c45\u9152\u5c4b\u3068\u3044\u3048\u307e\u3059\u3002\u30e1\u30cb\u30e5\u30fc\u306f\u8c4a\u5bcc\u3067\u3001\u660e\u592a\u5b50\u5165\u308a\u5375\u713c\u304d\u306f\u60f3\u50cf\u4ee5\u4e0a\u306b\u7f8e\u5473\u3067\u3057\u305f\u3002", "score": {"numeric": 3}, "poster": {"user": {"name": "\u539f\u572d\u52a9", "posted": 364}}} err2: JSONDecodeError('Expecting value: line 1 column 1 (char 0)',) <Response [504]>
###該当のソースコード
python
1#goocrawl.py 2 3def postKutikomiData(data): 4 base_url = "http://10.0.1.5" 5 url = base_url + "/api/review" 6 data["storeId"] = str(data["storeId"]) 7 data_json = json.dumps(data) 8 headers = {"Content-Type": "application/json"} 9 print('data_json: ', data_json, flush=True) 10 try: 11 r = requests.post(url, headers=headers, data=data_json) 12 except Exception as e: 13 print('conDB1: ','%r' % e, flush=True) 14 try: 15 if r.json()["message"] == "review_already_exist": 16 print("review_already_exist", flush=True) 17 pprint.pprint(r.json()) 18 return -1 19 except Exception as e: 20 print('conDB2: ', '%r' % e, r, flush=True) 21 return 1
python
1#Review.py 2 3class Review: 4 5 def on_get(self, req, res): 6 logging.info('Review[get]') 7 res.status = falcon.HTTP_200 8 res.body = json.dumps({'message': 'Hello'}) 9 10 def on_post(self, req, res): 11 logging.info('Review[post]') 12 body = req.stream.read() 13 try: 14 data = json.loads(body.decode('utf-8')) 15 logging.info('Review[post]:data %s' % str(data)) 16 except Exception as e: 17 res.status = falcon.HTTP_400 18 res.body = json.dumps({ 'message': str(e) }) 19 return 20 21 try: 22 # 口コミがすでに登録されていないかチェック 23 hashed = hashlib.sha256(data['content'].encode('utf-8')).hexdigest() 24 if REVIEWS.find_one({'hashed': hashed}): 25 res.body = json.dumps({'message': 'review_already_exist'}) 26 res.status = falcon.HTTP_400 27 return 28 29 # NLP 30 try: 31 data = nlp(data) 32 data['hashed'] = hashed 33 except Exception as e: 34 logging.error(e) 35 logging.error(traceback.format_exc()) 36 res.status = falcon.HTTP_500 37 res.body = json.dumps({ 'message': 'nlp_error: '+str(e) }) 38 39 store = STORES.find_one({'_id': objectid.ObjectId(data['storeId'])}) 40 if not store: 41 res.body = json.dumps({'message': 'invalid_storeId'}) 42 res.status = falcon.HTTP_400 43 return 44 45 data['tagIds'] = store['tagIds'] 46 data['storeId'] = store['_id'] 47 data['groupId'] = store['groupId'] 48 data['created'] = data['updated'] = datetime.datetime.utcnow() 49 if 'title' not in data: 50 data['title'] = data['content'][:20] 51 52 53 # 通知 54 tags = TAGS.find({'_id': {'$in': store['tagIds']}}) 55 group = DB.groups.find_one({'_id': store['groupId']}) 56 57 tagNames = [] 58 # Lineに送信 59 line_keys = store['lineKeysForReview'] 60 for tag in tags: 61 line_keys.extend(tag['lineKeysForReview']) 62 tagNames.append(str(tag['name'])) 63 line_keys.extend(group['lineKeysForReview']) 64 line_keys = list(set(line_keys)) 65 send2line(data, line_keys, tagNames) 66 67 # 保存 68 REVIEWS.insert_one(data) 69 res.body = json.dumps({'message': 'success'}) 70 return 71 except KeyError: 72 logging.error('KeyError', traceback.format_exc()) 73 res.status = falcon.HTTP_400 74 res.body = json.dumps({ 'message': 'missing_ key' }) 75 except Exception as e: 76 logging.error(e) 77 logging.error(traceback.format_exc()) 78 res.status = falcon.HTTP_500 79 res.body = json.dumps({ 'message': 'server_error' }) 80
###試したこと
jsonの形式が違うのかと思い、確かめて見るも、問題はなく、ローカルでテストすると問題なく通るのですが、サーバーにデプロイした後にサーバーで手動で実行すると上述のエラーが出ます。
また、口コミデータ中に'や"があるのかと思い、エスケープして見るも変化なし。しかもそれらが入っていない口コミでもエラーがでる。
サーバーにデプロイすると発生するのでサーバーの環境の問題かと思いデフォルトのpythonの文字コードを変えて見るも変化なし。
正直お手上げ状態です。
どうか、どなたかお導きください。
###補足情報(言語/FW/ツール等のバージョンなど)
サーバー
Ubuntu 16.04
EasyProcess==0.2.3
psutil==5.2.2
pymongo==3.4.0
PyVirtualDisplay==0.2.1
requests==2.14.2
selenium==3.4.1
ローカル
macOS Sierra 10.12.5
beautifulsoup4==4.6.0
bs4==0.0.1
falcon==1.2.0
gunicorn==19.7.1
lxml==3.8.0
mecab-python3==0.7
natto-py==0.8.0
pycparser==2.17
PyDispatcher==2.0.5
pymongo==3.4.0
pyOpenSSL==17.1.0
PyVirtualDisplay==0.2.1
queuelib==1.4.2
requests==2.14.2
selenium==3.4.2
サーバーで実行していて最初のうちはスムーズに動作するのですが、しばらくやっていると、中身のデータに関係なく不定期にこのエラーが発生し、その後全てのデータがこのエラーで弾かれてしまいます。
もしかすると、Azureのシステムの方でDosか何かと間違われたりしているのでしょうか??
試しにAWSに移植してみたのですが、同様のエラーが発生してしまいいよいよ行き詰ってきました。
以下関係ありそうな部分のソースコードです
https://github.com/nekosfc/goocrawler

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/10 23:10 編集