Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
LQ6H authored May 21, 2020
1 parent 32c1de4 commit f4aa4cd
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 20200521_duban-movie-analysis/movietest2/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class Movietest2Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()

# 电影 id
movie_id=scrapy.Field()

# 电影名称
movie_title=scrapy.Field()

# tag
movie_tag=scrapy.Field()

# 电影评分
movie_rate=scrapy.Field()

# 导演
movie_director=scrapy.Field()

# 类型
movie_types=scrapy.Field()

# 制片国家
movie_country=scrapy.Field()

# 片长
how_long=scrapy.Field()

# 用户名
user_name=scrapy.Field()

# 评论内容
content=scrapy.Field()

# 发布时间
release_time=scrapy.Field()

# 用户链接
user_link=scrapy.Field()

# 用户id
user_id=scrapy.Field()



105 changes: 105 additions & 0 deletions 20200521_duban-movie-analysis/movietest2/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals

class Movietest2SpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.

# Should return None or raise an exception.
return None

def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.

# Must return an iterable of Request, dict or Item objects.
for i in result:
yield i

def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.

# Should return either None or an iterable of Request, dict
# or Item objects.
pass

def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.

# Must return only requests (not items).
for r in start_requests:
yield r

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)


class Movietest2DownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.

# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None

def process_response(self, request, response, spider):
# Called with the response returned from the downloader.

# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response

def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.

# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)



96 changes: 96 additions & 0 deletions 20200521_duban-movie-analysis/movietest2/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
import json

class Movietest2Pipeline(object):
collection_name="test"

def __init__(self,mongo_uri,mongo_db):

self.mongo_uri=mongo_uri
self.mongo_db=mongo_db

@classmethod
def from_crawler(cls,crawler):

return cls(
mongo_uri=crawler.settings.get('MONGO_URI'),
mongo_db=crawler.settings.get('MONGO_DATABASE','Movie')
)


def open_spider(self,spider):

self.client=pymongo.MongoClient(self.mongo_uri)
self.db=self.client[self.mongo_db]


def close_spider(self,spider):

self.client.close()


def process_item(self, item, spider):

self.db[self.collection_name].insert(dict(item))
return item


class Movietest2FilePipeline(object):

def open_spider(self,spider):
#self.file=open("review_final.json","a",encoding="utf-8")
self.file = open("test.json", "a", encoding="utf-8")

def process_item(self,item,spider):

line = json.dumps(dict(item), ensure_ascii=False) + "\n"

self.file.write(line)

return item

def close_spider(self,spider):

self.file.close()


"""
class Movietest2CsvPipeline(object):
def __init__(self):
self.f=open('review.csv','a',encoding="utf-8")
self.exportre=CsvItemExporter(
self.f,
include_headers_line=True,
encoding='utf-8'
)
def open_spider(self,spider):
pass
def process_item(self,item,spider):
self.exportre.export_item(item)
return item
def close_spider(self,spider):
self.f.close()
"""




96 changes: 96 additions & 0 deletions 20200521_duban-movie-analysis/movietest2/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-

# Scrapy settings for movietest2 project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'movietest2'

SPIDER_MODULES = ['movietest2.spiders']
NEWSPIDER_MODULE = 'movietest2.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'movietest2 (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
'Cookie':'bid=9JMhtyhKwCI; gr_user_id=dfc4a657-74ee-4905-90d1-afdcc278240c; _vwo_uuid_v2=D817A52A7B1A438F346CA5145718FC70D|11c7965ceae6dae41bda579ad8cc07ad; _ga=GA1.2.704102812.1568950460; ll="118254"; __yadk_uid=KJcw8kSGqgo0bzV13fiv89IQHblmzQnp; trc_cookie_storage=taboola%2520global%253Auser-id%3D6db6843a-c51d-4dd1-929b-c64a9d65c502-tuct47b1992; __gads=ID=3f19cf254ba723db:T=1572747084:S=ALNI_Mao2HHyRrl7R9fbqqQ-8L9y6ur9jw; douban-fav-remind=1; push_noty_num=0; push_doumail_num=0; viewed="34839848_26337727_30136932_26342364_1234142_26416562_2061116_26827295"; __utmz=30149280.1588833276.3.2.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; dbcl2="175718936:oI/7dGsQRCM"; __utmv=30149280.17571; ck=eH3o; __utmc=30149280; __utmc=223695111; ct=y; __utma=30149280.704102812.1568950460.1589080211.1589087550.8; __utmb=30149280.2.10.1589087550; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1589087552%2C%22https%3A%2F%2Fwww.douban.com%2F%22%5D; _pk_ses.100001.4cf6=*; __utma=223695111.704102812.1568950460.1589080211.1589087552.41; __utmb=223695111.0.10.1589087552; __utmz=223695111.1589087552.41.22.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _pk_id.100001.4cf6=3297dbc060884462.1570180143.41.1589088238.1589080924.'
}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'movietest2.middlewares.Movietest2SpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'movietest2.middlewares.Movietest2DownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'movietest2.pipelines.Movietest2Pipeline': 300,
'movietest2.pipelines.Movietest2FilePipeline':400
}


MONGO_URI='localhost:27017'
MONGO_DATABASE='Movie'


# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
4 changes: 4 additions & 0 deletions 20200521_duban-movie-analysis/movietest2/spiders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit f4aa4cd

Please sign in to comment.