search

Home  >  Q&A  >  body text

python - Why does the program stop executing at this point: if isinstance(item, AnItem): This sentence is obviously true, but it will not be executed at this point.

I defined AnItem in items

It is also defined in spider.py using item = AnItem().

Why the above: if isinstance(item, AnItem): What follows this sentence will not be executed.

Explanation: if isinstance(item, AnItem): It is false, I don’t know why?

I look at other people’s code. There are also examples on the Internet that are written like this

I don’t know what I wrote wrong?

class AnPipeline(object):
    def process_item(self, item, spider):
        print ('process_item1111111111111111111111111111')
        if isinstance(item, AnItem):
            print ('process_item222222222222222222222222222')
            title = item['title']
            content = item['content']
            Sql.insert_data_all(title, content)
            print (u'存完一条信息aaaaaaaaaaaa')
            return item
漂亮男人漂亮男人2757 days ago894

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-18 10:59:41

    Is it true or false? Can’t you output it to see?

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-18 10:59:41

    if isinstance(item, AnItem):

        print ('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')
    else:
        print ('jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj')
    

    Tested it using the above statement in pipelines.py: output jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
    I don’t know what went wrong.
    According to normal logic, it is defined in spiders.py: item = ZbaqcnItem()
    Everything that should be imported is imported. Then it's in pipelines.py. It should be true?

    Now I feel that there should be a problem with the import item?

    I import like this in anspiders.py:
    from aqccncn.items import AnItem
    Always prompt an error. ImportError: No module named items
    I searched online for a long time and couldn’t find a feasible solution.
    Have no choice but to use the following method.

    Then I created items.py in the spiders file
    I imported it using from items import AnItem.

    Then the later if isinstance(item, AnItem): is False.

    Is this the reason?

    Finally——————————————————————————————————————————————
    Modified at 2016/04/29 18:04:

    ——————————————————————————————

    Thank you all. Already solved. It turns out that at the beginning, from aaaa.items import AnItem always prompted an error. I created an items.py in the spiders file again, so the imported items.py is not the same, so the AnItem() is not the same either.

    But there is another question:

    If you create the items.py file in the same directory as the settgins.py file in the spiders folder. Why import like this: from AnAnAn.items import AnItem will prompt: ImportError: No module named AnAnAn.items?
    In the AnAnAn folder, there is obviously the items.py generated by default, and the corresponding code is added to it.

    What’s the problem with this? Thank you.

    reply
    0
  • Cancelreply