|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
基本思路:
1.先从首页获取到每部电影的link(链接),年份/地区/类型,标签。
2.从详情页获取,导演、编剧,演员、简介等信息。
3.通过判断进行翻页操作。重复第一步,第二部。
代码如下
gogo.py
- # -*- coding: utf-8 -*-
- import scrapy
- from DouBanTop250.items import Doubantop250Item
- from scrapy.http import Request
- import re
- class GogoSpider(scrapy.Spider):
- name = 'gogo'
- #allowed_domains = ['https://movie.douban.com/top250']
- start_urls = ['https://movie.douban.com/top250?start=0']
- def parse(self, response):
- #print(response.text)
- items = []
- url_list = response.xpath('//*[@id="content"]/div/div[1]/ol')#获取内容列表
- item = Doubantop250Item()
- for url in url_list:
- item['link'] = url.xpath('.//@href').extract()#电影详情页链接
- item['yead'] = url.xpath('.//div[2]/div[2]/p[1]/text()[2]').extract()#临时参数,用来获取年份/地区/类型
- item['label'] = url.xpath('.//div[2]/div[2]/p[2]/span/text()').extract() # 标签
- item['score'] = url.xpath('.//div[2]/div[2]/div/span[2]/text()').extract() # 评分
- items.append(item)
- print(item['link'],item['yead'],item['label'],item['score'])
- #yield Request(url=item['link'], meta={'item1': item}, callback=self.parse_one)
- for a in item['yead']:#把年份/地区/类型去除标签,并取出来
- year = a.strip() #去除空格
- year = year.lstrip() # 1994 / 美国 / 犯罪 剧情
- year = year.replace(' ', '\\') # 1995 / 香港中国大陆 / 喜剧爱情奇幻冒险
- year = year.replace('/', '')
- year = year.replace('\\', '/')
- year = year.split() # ['1994', '美国', '犯罪/剧情']
- item['Released'] = year[0] # 上映时间
- item['Production'] = year[1] # 上映地区
- item['types'] = year[2] #类型
- items.append(item)
- print(item['Released'],item['Production'],item['types'])#1994 美国 犯罪/剧情
- #yield Request(url=item['link'], meta={'item1': item}, callback=self.parse_one)
- # 问题在上面这一句,报错说,函数parse_one不能接受列表的link,只接受字符串的link
- def parse_one(self, response):#进入详情页获取数据
- item = Doubantop250Item()
- item2 = response.meta['item1']
- item['title'] = response.xpath('//*[@id="content"]/h1/span[1]/text()').extract()[0] # 标题
- item['director'] = response.xpath('//*[@id="info"]/span[1]/span[@class="attrs"]/a/text()').extract()[0] # 导演
- item['sxreenwriter'] = response.xpath('//*[@id="info"]/span[2]/span[2]/a/text()').extract()[0] # 编剧 no
- req = r'<span class="pl">语言:</span> (.*?)<br/>\n '
- item['Language'] = re.findall(req, response.text, re.S)[0] # 语言
- req = r'<span property="v:runtime" content=".*?">(.*?)</span>'
- item['Length'] = re.findall(req, response.text)[0] # 片长时间/分钟
- req = r'<span class="pl">又名:</span> (.*?)<br/>\n '
- item['Alias'] = re.findall(req, response.text, re.S)[0] # 别名
- req = r'<span class="all hidden">(.*?)</span>|<span property="v:summary" class="">(.*?)</span>'
- item['Introduction'] = re.findall(req, response.text, re.S) # 剧情简介
- print(item['title'],item['director'],item['sxreenwriter'],['Language'],item['Length'],item['Alias'],item['Introduction'])
- #yield item
复制代码
items.py
- import scrapy
- class Doubantop250Item(scrapy.Item):
- # define the fields for your item here like:
- # name = scrapy.Field()
- link = scrapy.Field() # 链接
- title = scrapy.Field() # 电影名
- director = scrapy.Field() # 导演
- sxreenwriter = scrapy.Field() # 编剧
- types = scrapy.Field() # 电影类型
- Production = scrapy.Field() # 制片地区
- Language = scrapy.Field() # 语言
- Released = scrapy.Field() # 上映时间
- Length = scrapy.Field() # 片长时间/分钟
- Alias = scrapy.Field() # 别名
- Introduction = scrapy.Field() # 剧情简介
- label = scrapy.Field() # 标签
- score = scrapy.Field() # 评分
- yead = scrapy.Field() # 临时参数
复制代码
问题出在把第一个函数中的link是列表,传入parse_one(self, response)时要求link是字符串。
该如何改写让link是字符串呢??
代码下载
链接: https://pan.baidu.com/s/1QBt7InC0obPb9nW_RsX02A 密码: 847r
|
|