鱼C论坛

 找回密码
 立即注册
查看: 1776|回复: 1

[已解决]关于爬取《豆瓣电影Top250》遇到的问题

[复制链接]
发表于 2018-4-16 18:39:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
基本思路:
1.先从首页获取到每部电影的link(链接),年份/地区/类型,标签。
2.从详情页获取,导演、编剧,演员、简介等信息。
3.通过判断进行翻页操作。重复第一步,第二部。

代码如下
gogo.py
  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from DouBanTop250.items import Doubantop250Item
  4. from scrapy.http import Request
  5. import re

  6. class GogoSpider(scrapy.Spider):
  7.     name = 'gogo'
  8.     #allowed_domains = ['https://movie.douban.com/top250']
  9.     start_urls = ['https://movie.douban.com/top250?start=0']

  10.     def parse(self, response):
  11.         #print(response.text)
  12.         items = []
  13.         url_list = response.xpath('//*[@id="content"]/div/div[1]/ol')#获取内容列表
  14.         item = Doubantop250Item()
  15.         for url in url_list:
  16.             item['link'] = url.xpath('.//@href').extract()#电影详情页链接
  17.             item['yead'] = url.xpath('.//div[2]/div[2]/p[1]/text()[2]').extract()#临时参数,用来获取年份/地区/类型
  18.             item['label'] = url.xpath('.//div[2]/div[2]/p[2]/span/text()').extract() # 标签
  19.             item['score'] = url.xpath('.//div[2]/div[2]/div/span[2]/text()').extract() # 评分
  20.             items.append(item)
  21.             print(item['link'],item['yead'],item['label'],item['score'])
  22.             #yield Request(url=item['link'], meta={'item1': item}, callback=self.parse_one)


  23.         for a in item['yead']:#把年份/地区/类型去除标签,并取出来
  24.             year = a.strip() #去除空格
  25.             year = year.lstrip()  # 1994 / 美国 / 犯罪 剧情
  26.             year = year.replace(' ', '\\')  # 1995 / 香港中国大陆 / 喜剧爱情奇幻冒险
  27.             year = year.replace('/', '')
  28.             year = year.replace('\\', '/')
  29.             year = year.split()  # ['1994', '美国', '犯罪/剧情']
  30.             item['Released'] = year[0]  # 上映时间
  31.             item['Production'] = year[1]  # 上映地区
  32.             item['types'] = year[2]  #类型
  33.             items.append(item)
  34.             print(item['Released'],item['Production'],item['types'])#1994 美国 犯罪/剧情
  35.             #yield Request(url=item['link'], meta={'item1': item}, callback=self.parse_one)
  36.             # 问题在上面这一句,报错说,函数parse_one不能接受列表的link,只接受字符串的link

  37.     def parse_one(self, response):#进入详情页获取数据
  38.         item = Doubantop250Item()
  39.         item2 = response.meta['item1']
  40.         item['title'] = response.xpath('//*[@id="content"]/h1/span[1]/text()').extract()[0]  # 标题

  41.         item['director'] = response.xpath('//*[@id="info"]/span[1]/span[@class="attrs"]/a/text()').extract()[0]  # 导演

  42.         item['sxreenwriter'] = response.xpath('//*[@id="info"]/span[2]/span[2]/a/text()').extract()[0]  # 编剧 no

  43.         req = r'<span class="pl">语言:</span> (.*?)<br/>\n '
  44.         item['Language'] = re.findall(req, response.text, re.S)[0]  # 语言

  45.         req = r'<span property="v:runtime" content=".*?">(.*?)</span>'
  46.         item['Length'] = re.findall(req, response.text)[0]  # 片长时间/分钟

  47.         req = r'<span class="pl">又名:</span> (.*?)<br/>\n  '
  48.         item['Alias'] = re.findall(req, response.text, re.S)[0]  # 别名

  49.         req = r'<span class="all hidden">(.*?)</span>|<span property="v:summary" class="">(.*?)</span>'
  50.         item['Introduction'] = re.findall(req, response.text, re.S)  # 剧情简介
  51.         print(item['title'],item['director'],item['sxreenwriter'],['Language'],item['Length'],item['Alias'],item['Introduction'])
  52.         #yield item
复制代码


items.py

  1. import scrapy


  2. class Doubantop250Item(scrapy.Item):
  3.     # define the fields for your item here like:
  4.     # name = scrapy.Field()
  5.     link = scrapy.Field()          # 链接

  6.     title = scrapy.Field()         # 电影名

  7.     director = scrapy.Field()      # 导演

  8.     sxreenwriter = scrapy.Field()  # 编剧

  9.     types = scrapy.Field()         # 电影类型

  10.     Production = scrapy.Field()    # 制片地区

  11.     Language = scrapy.Field()      # 语言

  12.     Released = scrapy.Field()      # 上映时间

  13.     Length = scrapy.Field()        # 片长时间/分钟

  14.     Alias = scrapy.Field()         # 别名

  15.     Introduction = scrapy.Field()  # 剧情简介

  16.     label = scrapy.Field()         # 标签

  17.     score = scrapy.Field()         # 评分

  18.     yead = scrapy.Field()          # 临时参数

复制代码



问题出在把第一个函数中的link是列表,传入parse_one(self, response)时要求link是字符串。
该如何改写让link是字符串呢??
代码下载
链接: https://pan.baidu.com/s/1QBt7InC0obPb9nW_RsX02A 密码: 847r
最佳答案
2018-4-17 10:19:49
使用循环迭代啊,把你的列表迭代成字符串不就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-17 10:19:49 | 显示全部楼层    本楼为最佳答案   
使用循环迭代啊,把你的列表迭代成字符串不就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-21 02:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表