import scrapy
import json
import codecs
import re
class commentspider(scrapy.spider):
name = "comment"
start_urls = [
]def parse(self, response):
# filename = "firstpagecomment.json"
# data = json.loads(response.text)
# print(data['data']['data'][0]['text'])
# data = json.dumps(data, ensure_ascii = false)
# # print(data.data[0].text)
# # print(data)
# with codecs.open(filename, 'w+', encoding='utf-8') as f:
# f.write(data)
""":param response: response物件
"""result = json.loads(response.text)
if result.get('ok') and result.get('data').get('data') and len(result.get('data').get('data')):
comment_list = result.get('data').get('data')
for comment in comment_list:
if (comment.get('text')):
comment_text = re.sub(r'', '', comment.get('text'))
# print("1",comment_text)
body =
yield scrapy.http.request('',(這是獲取的accessed_token加在後面)
callback = self.requestcallback,
method='post',
body = json.dumps(body))
def requestcallback(self, response):
result = json.loads(response.text)
if result.get('text') and result.get('items'):
# print(result.get('text'), '積極情感' if result.get('items')[0].get('positive_prob') > result.get('items')[0].get('negative_prob') else '消極情感')
print('積極情感' if result.get('items')[0].get('positive_prob') > result.get('items')[0].get('negative_prob') else '消極情感')
# else:
# print('error', result)
然後修改items.py裡面的內容:
import scrapy
class weibospideritem(scrapy.item):
# define the fields for your item here like:
weibo_id = scrapy.field()
text = scrapy.field()
pass
四、執行在pycharm終端輸入scrapy crawl comment
然後稍等一會就可以在終端看見輸出結果,如下圖:
中途用postman 除錯的截圖如下:
到這裡就大致完成了,本人實屬菜鳥,是在朋友指導下才得以完成。
基於Python的情感分析案例
情感極性分析的目的是對文字進行褒義 貶義 中性的判斷。在大多應用場景下,只分為兩類。例如對於 喜愛 和 厭惡 這兩個詞,就屬於不同的情感傾向。示例1 好評 示例2 差評 讀取文字檔案 def text f1 open e 工作檔案 情感分析案例1 good.txt r encoding utf 8 ...
第12課 完全基於情感詞典的文字情感分析
下面我們通過以下幾點來介紹中文自然語言處理情感分析 中文情感分析方法簡介 基於標註好的情感詞典來計算情感值 pytreebank 繪製情感樹 股吧資料情感分類。情感傾向可認為是主體對某一客體主觀存在的內心喜惡,內在評價的一種傾向。它由兩個方面來衡量 乙個情感傾向方向,乙個是情感傾向度。目前,情感傾向...
基於sklearn的感知機python3
首先,本文還是選用python裡面自帶的digits資料集 from sklearn.datasets import load digits digits load digits 資料標準化 from sklearn.preprocessing import standardscaler scale...