豆瓣电影TOP250的网页解析

调试过程

1.DEBUG: Crawled (403) 的解决办法

首先我们按照之前的经验输入

scrapy shell https://movie.douban.com/top250

输入后,出现了DEBUG: Crawled (403) ,表示网站采用了防爬技术anti-web-crawling technique,比较简单即会检查用户代理(User Agent)信息。

输入后出现DEBUG 403

之后,我们进行了代码输入对影片名,进行了爬取,发现结果为空。

>>>response.xpath("//ol[@class='grid_view']/li[1]/div[@class='item']/div[@class='info']/div[@class='hd']/a/span/text()").extract()
> []

通过检索我们发现,出现403的错误时应该键入

scrapy shell -s USER_AGENT='Mozilla/5.0' https://movie.douban.com/top250

显示DEBUG: Crawled (200),是正常情况的调试信息

 2018-05-15 22:04:09 [scrapy.core.engine] DEBUG: Crawled (200) (referer: None)

2.extract()与extract_first()

extract_first()返回字符串

extract()返回数组

3. 标签的不规律导致爬取困难

电影标题对应的标签(<span class="title">)数量是变化的,即一个电影可能对应不同数量个title。

电影1的标题
电影2的标题

针对此问题,我们考虑的方案1是使title变量为一个多维的列表(list),并在最终输出结果时通过遍历此列表来输出结果。但在遍历过程中一直bug。

后来,我们想出了一个更简单但有效的办法:由于电影标题对应的标签始终为1-2个,我们设了两个变量,分别赋值两个标签,在输出最终结果时通过if语句判定title2是否有值并输出。

最终代码

xpath代码

for movie in response.xpath("//ol[@class='grid_view']/li"):
  title=movie.xpath("div[@class='item']/div[@class='info']/div[@class='hd']/a/span[@class='title']/text()").extract_first()
  title2=movie.xpath("div[@class='item']/div[@class='info']/div[@class='hd']/a/span[@class='title'][2]/text()").extract_first()
  other=movie.xpath("div[@class='item']/div[@class='info']/div[@class='hd']/a/span[@class='other']/text()").extract_first()
  pic=movie.xpath("div[@class='item']/div[1]/a/img/@src").extract_first()
  desc2=movie.xpath("div[@class='item']/div[2]/div[2]/p[1]/text()").extract()
  score=movie.xpath("div[@class='item']/div[@class='info']/div[@class='bd']/div[@class='star']/span[2]/text()").extract_first()
  scoreperson=movie.xpath("div[@class='item']/div[@class='info']/div[@class='bd']/div[@class='star']/span[4]/text()").extract_first()
  review=movie.xpath("div[@class='item']/div[@class='info']/div[@class='bd']/p[@class='quote']/span/text()").extract_first()
  print "标题:",title,
  if title2:
      print title2,
  print "其他标题:",other,
  print "图片:",pic,
  print "简介:",desc2[0],
  print "描述信息:",desc2[1],
  print "评分:",score,
  print "人数:",scoreperson
  print "评论:",review

css代码

for movie in response.css("ol.grid_view li"):
  title=movie.css("div.hd a span::text").extract()[0]
  title2=movie.css("div.hd a span::text").extract()[1]
  other=movie.css("div.hd a span.other::text").extract_first()
  pic=movie.css("div.pic a img::attr(src)").extract_first()
  desc=movie.css("div.bd p::text").extract()
  score=movie.css("span.rating_num::text").extract_first()
  scoreperson=movie.css("div.star span::text").extract()
  review=movie.css("p.quote span.inq::text").extract_first()
  print "名称:",title,
  if title2:
      print title2,
  print other,
  print "图片:",pic,
  print "演职员表:",desc[0],
  print "类别:",desc[1],
  print "评分:",score,
  print "人数:",scoreperson[1],
  print "评论:",review

运行结果

运行结果
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,410评论 19赞 139
  • 《裕语言》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 29,189评论 5赞 20
  • Python开发简单爬虫(Python2.X版本,Eclipse工具) 一、爬虫介绍 爬虫调度端:启动、停止爬虫,...
    凛0_0阅读 2,256评论 2赞 10
  • 一个人在一件事上说话突然强势起来只会有两个原因 第一,他是真的有理 第二,他是常有理,是在遮掩什么见不到人的事情
    君末言阅读 171评论 0赞 1
  • 一、这周的目标达成情况 1、英语 精读完《棚车少年1》,并通过考核 达成 2、运动 倒立突破至3分钟,可循序渐进。...
    黄道凯阅读 378评论 0赞 1

友情链接更多精彩内容