【问题标题】:Python TypeError: 'module' object does not support item assignmentPython TypeError:“模块”对象不支持项目分配
【发布时间】:2020-11-06 14:09:05
【问题描述】:

我正在尝试使用 python 和 pycharm IDE 编写网络爬虫。我已经编写了一个基本代码,用于使用“项目”来抓取网站的三个元素。现在我面临错误“TypeError:'模块'对象不支持项目分配”。


import scrapy
from .. import items
from ..items import QuotesItem
class QuotesSpider (scrapy.Spider):
    name = 'Quotes'
    start_urls = [
        'https://quotes.toscrape.com/'
    ]
    def _parse(self, response):
        div_tags = response.css('div.quote')
        for x in div_tags :
            title = x.css('span.text::text').extract()
            author = x.css('.author::text').extract()
            tags = x.css('.tag::text').extract()
            items['title_item'] = title
            items['author_item'] = author
            items['tag_item'] = tags
    
            yield items*****

【问题讨论】:

  • items 是一个模块(对import QuotesItem)还是一个dict

标签: python scrapy pycharm web-crawler


【解决方案1】:

我认为在这个函数中,你有相反的变量赋值。应该是这样的

    def _parse(self, response):
        div_tags = response.css('div.quote')
        for x in div_tags :
            title = x.css('span.text::text').extract()
            author = x.css('.author::text').extract()
            tags = x.css('.tag::text').extract()
            title = items['title_item']
            author = items['author_item']
            tags = items['tag_item']
    
            yield items*****

【讨论】:

  • 我希望这会给出“TypeError: 'module' object is not subscriptable”(如果 items 是一个模块)。
猜你喜欢
  • 1970-01-01
  • 2016-10-12
  • 2016-07-31
  • 2014-02-18
  • 2017-03-26
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多