AttributeError: ‘str‘ object has no attribute ‘iter‘--Scrapy框架问题解决

 

 使用scrapy框架的crawl模板爬取豆瓣250报错却还可以正常爬取数据,但是翻页功能无法实现,spider.py代码:

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import time
import random

class CrawlMovieSpider(CrawlSpider):
    name = 'crawl_movie'
    allowed_domains = ['douban.com']
    start_urls = ['https://movie.douban.com/top250?start=0&filter=']

    rules = (
        # 正则提取详情页的url :https://movie.douban.com/subject/1292052/
        Rule(LinkExtractor(allow=r'^https://movie.douban.com/subject/\d+/$'), callback='parse_item', follow=False),
        # xpath提取 另外一种xpath语法://a[text()='后页>']/@href   //span[@class="next"]/a/@href
        Rule(LinkExtractor(restrict_xpaths=('//span[@class="next"]/a/@href')),  follow=True),# # follow=True意味着一直按照这个规则提取url,,构建请求对象
    )
    # time.sleep(random.randint(1,2))



    def parse_item(self, response):
        item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        # 电影名称
        title = response.xpath('//h1/span[1]/text()').get()  # 只有单个数据,所以用get()提取出字符串
        print(title)
        # 电影评分
        score = response.xpath('//strong/text()').get()
        print(score)
        # 电影简介:有两种xpath提取语法
        text_list = response.xpath('//div[@class="indent"]/span[1]/text()|//div[@class="indent"]/span[2]/text()').extract()
        text = ''.join(text_list)
        text = text.replace('\u3000','')
        text = text.replace(' ','')
        text = text.replace('\n','')
        print(text)
        # 存入数据
        item['title'] = title
        item['score'] = score
        item['text'] = text
        return item

这个错误的原因是 restrict_xpaths 应该指向元素,也就是直接链接或包含链接的容器,而不是它的属性,而我们的代码中用的是 a/@href

更详细的讲解可以参考:https://www.cnblogs.com/lei0213/p/8097515.html

如果对你有所帮助,麻烦点个赞鼓励一下,谢谢!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无限乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值