Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 碎片数据流、项目和项目加载器_Python_Python 3.x_Python 2.7_Scrapy - Fatal编程技术网

Python 碎片数据流、项目和项目加载器

Python 碎片数据流、项目和项目加载器,python,python-3.x,python-2.7,scrapy,Python,Python 3.x,Python 2.7,Scrapy,我正在查看残缺不全的文档中的页面,但是我仍然有一些关于数据和/或控制流的问题 刮擦式架构 零碎项目的默认文件结构 item.py 我想这会变成 import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Field() stock = scrapy.Field() last_updated = scrapy.Field(serializer=str) 因此,在尝

我正在查看残缺不全的文档中的页面,但是我仍然有一些关于数据和/或控制流的问题

刮擦式架构

零碎项目的默认文件结构

item.py

我想这会变成

import scrapy

class Product(scrapy.Item):
    name = scrapy.Field()
    price = scrapy.Field()
    stock = scrapy.Field()
    last_updated = scrapy.Field(serializer=str)
因此,在尝试填充
Product
实例的未声明字段时会引发错误

>>> product = Product(name='Desktop PC', price=1000)
>>> product['lala'] = 'test'
Traceback (most recent call last):
    ...
KeyError: 'Product does not support field: lala'
问题1

如果我们在
items.py
中创建了
class CrowdfundingItem
,我们的爬虫程序在何处、何时以及如何意识到
items.py

这是在

  • \uuuu init\uuuuu.py
  • my_crawler.py
  • mycrawler.py的
    def\uuuu init\uuuu()
  • settings.py
  • pipelines.py
  • def\uu init\uu(self,dbpool)
    of
    pipelines.py
  • 其他地方
问题2

一旦我声明了一个项目,例如
Product
,那么如何通过在类似于下面的上下文中创建
Product
的实例来存储数据

import scrapy

class MycrawlerSpider(CrawlSpider):
    name = 'mycrawler'
    allowed_domains = ['google.com']
    start_urls = ['https://www.google.com/']
    def parse(self, response):
        options = Options()
        options.add_argument('-headless')
        browser = webdriver.Firefox(firefox_options=options)
        browser.get(self.start_urls[0])
        elements = browser.find_elements_by_xpath('//section')
        count = 0
        for ele in elements:
             name = browser.find_element_by_xpath('./div[@id="name"]').text
             price = browser.find_element_by_xpath('./div[@id="price"]').text

             # If I am not sure how many items there will be,
             # and hence I cannot declare them explicitly,
             # how I would go about creating named instances of Product?

             # Obviously the code below will not work, but how can you accomplish this?

             count += 1
             varName + count = Product(name=name, price=price)
             ...
最后,假设我们完全放弃命名
产品
实例,而只是创建未命名的实例

for ele in elements:
    name = browser.find_element_by_xpath('./div[@id="name"]').text
    price = browser.find_element_by_xpath('./div[@id="price"]').text
    Product(name=name, price=price)

如果这些实例确实存储在某个地方,它们存储在哪里?通过这种方式创建实例,是否不可能访问它们?

使用
项是可选的;它们只是声明数据模型和应用验证的方便方法。您也可以改用普通的
dict

如果您选择使用
,则需要导入该项以便在spider中使用。它不是自动发现的。就你而言:

from items import CrowdfundingItem
当爬行器在每个页面上运行
parse
方法时,您可以将提取的数据加载到
项或
dict
中。装载后,
产生
它,然后将其传递回scrapy引擎,以便在管道或出口商中进行下游处理。这就是scrapy如何“存储”您刮取的数据

例如:

yield Product(name='Desktop PC', price=1000) # uses Item
yield {'name':'Desktop PC', 'price':1000} # plain dict

出于某种原因,它以前不让我屈服。它与
-t basic
配合得很好,但出于某种原因,它不是
-t crawl
:如果它能让我以后的生活更轻松的话,我不介意学习如何使用条目,特别是在处理刮取的数据并将其插入数据库时。当然,我可以
产生{}
,但是,当我尝试运行
scrapy crawl blah-o blah.json
时,它只会弹出一个空白的json文件……无论是使用
还是
dict
,生成的项都将在
管道
(您可以将数据库存储逻辑放在这里)中提供给
导出程序
(在您的示例中,它将它们写入
blah.json
)好的,那么文档中提到的项目有什么“方便”呢?
from items import CrowdfundingItem
yield Product(name='Desktop PC', price=1000) # uses Item
yield {'name':'Desktop PC', 'price':1000} # plain dict