【问题标题】:Scrapy using files.middleware downloads given file without extensionScrapy 使用 files.middleware 下载不带扩展名的给定文件
【发布时间】:2021-08-02 08:02:56
【问题描述】:

我想自动化一些文件交换。我需要从网站下载一个 .csv 文件,该文件通过身份验证进行保护,然后您才能开始下载。

首先,我想尝试使用 wget 下载文件,但我没有管理,所以我切换到了scrapy,一切正常,我的身份验证和下载,但是文件没有扩展名 -.-'

这是我的蜘蛛的 sn-p:

    def after_login(self, response):
        accview = response.xpath('//span[@class="user-actions welcome"]')
        if accview:
            print('Logged in')
            file_url = response.xpath('//article[@class="widget-single widget-shape-widget widget"]/p/a/@href').get()
            file_url = response.urljoin(file_url)
            items = StockfwItem()
            items['file_urls'] = [file_url]
            yield items

我的设置.py:

ITEM_PIPELINES = {'scrapy.pipelines.files.FilesPipeline': 1}

items.py:

    file_urls = scrapy.Field()
    files = scrapy.Field()

我确定我的蜘蛛存在问题的原因是,如果我通过浏览器常规下载文件,它总是以常规 csv 文件的形式出现。

当我尝试打开下载的文件(文件名在 sha1 中散列)时,我得到以下 error_msg:

  File "/usr/lib/python3.6/csv.py", line 111, in __next__
    self.fieldnames
  File "/usr/lib/python3.6/csv.py", line 98, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: line contains NULL byte

另外,当我用 notepad++ 打开下载的文件并将编码保存为 utf-8 时,它可以正常工作...

scrapy 控制台输出:

{'file_urls': ['https://floraworld.be/Servico.Orchard.FloraWorld/Export/Export']                                                                                                                                                             ,
 'files': [{'checksum': 'f56c6411803ec45863bc9dbea65edcb9',
            'path': 'full/cc72731cc79929b50c5afb14e0f7e26dae8f069c',
            'status': 'downloaded',
            'url': 'https://floraworld.be/Servico.Orchard.FloraWorld/Export/Expo                                                                                                                                                             rt'}]}
2021-08-02 10:00:30 [scrapy.core.engine] INFO: Closing spider (finished)
2021-08-02 10:00:30 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 2553,
 'downloader/request_count': 4,
 'downloader/request_method_count/GET': 2,
 'downloader/request_method_count/POST': 2,
 'downloader/response_bytes': 76289,
 'downloader/response_count': 4,
 'downloader/response_status_count/200': 3,
 'downloader/response_status_count/302': 1,
 'elapsed_time_seconds': 20.892172,
 'file_count': 1,
 'file_status_count/downloaded': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2021, 8, 2, 8, 0, 30, 704638),
 'item_scraped_count': 1,
 'log_count/DEBUG': 6,
 'log_count/INFO': 10,
 'log_count/WARNING': 1,
 'memusage/max': 55566336,
 'memusage/startup': 55566336,
 'request_depth_max': 1,
 'response_received_count': 3,
 'scheduler/dequeued': 4,
 'scheduler/dequeued/memory': 4,
 'scheduler/enqueued': 4,
 'scheduler/enqueued/memory': 4,
 'splash/render.html/request_count': 1,
 'splash/render.html/response_count/200': 1,
 'start_time': datetime.datetime(2021, 8, 2, 8, 0, 9, 812466)}
2021-08-02 10:00:30 [scrapy.core.engine] INFO: Spider closed (finished)

下载文件的sn-p并在ubuntu服务器上用vim打开:

"^@A^@r^@t^@i^@c^@l^@e^@C^@o^@d^@e^@"^@;^@"^@D^@e^@s^@c^@r^@i^@p^@t^@i^@o^@n^@"^@;^@"^@B^@B^@"^@;^@"^@K^@T^@"^@;^@"^@S^@i^@z^@e^@"^@;^@"^@P^@r^@i^@c^@e^@"^@;^@"^@S^@t^@o^@c^@k^@"^@;^@"^@D^@e^@l^@i^@v^@e^@r^@y^@D^@a^@t^@e^@"^@^M^@
^@"^@1^@0^@0^@0^@L^@"^@;^@"^@A^@l^@o^@e^@ ^@p^@l^@a^@n^@t^@ ^@x^@ ^@2^@3^@ ^@l^@v^@s^@"^@;^@"^@4^@"^@;^@"^@4^@"^@;^@"^@6^@5^@"^@;^@"^@4^@6^@,^@7^@7^@"^@;^@"^@1^@1^@8^@,^@0^@0^@0^@0^@0^@"^@;^@"^@"^@^M^@
^@"^@1^@0^@0^@0^@M^@"^@;^@"^@A^@l^@o^@e^@ ^@p^@l^@a^@n^@t^@ ^@x^@ ^@1^@7^@ ^@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@3^@0^@/^@0^@9^@/^@2^@0^@2^@1^@"^@^M^@
^@"^@1^@0^@0^@5^@N^@"^@;^@"^@N^@a^@t^@u^@r^@a^@l^@ ^@s^@t^@r^@e^@l^@i^@t^@z^@i^@a^@ ^@w^@/^@p^@o^@t^@"^@;^@"^@1^@"^@;^@"^@1^@"^@;^@"^@1^@3^@0^@"^@;^@"^@2^@0^@7^@,^@4^@4^@"^@;^@"^@1^@4^@,^@0^@0^@0^@0^@0^@"^@;^@"^@1^@/^@1^@2^@/^@2^@0^@2^@1^@"^@^M^@

这是什么鬼??

当我将文件名更改为file.csv时,将文件下载到我的windoof桌面并再次使用notepad++打开它,它看起来不错:

"ArticleCode";"Description";"BB";"KT";"Size";"Price";"Stock";"DeliveryDate"
"1000L";"Aloe plant x 23 lvs";"4";"4";"65";"46,77";"118,00000";""
"1000M";"Aloe plant x 17 lvs";"4";"12";"50";"32,61";"206,00000";""
"1000S";"Aloe plant x 16 lvs";"4";"24";"40";"22,32";"-6,00000";"23/08/2021"
"1002M";"BATON PLANT 67 CM W/POT";"2";"6";"67";"22,42";"33,00000";"5/09/2021"
"1002S";"BATON PLANT 42 CM W/POT";"4";"12";"42";"10,54";"-95,00000";"5/09/2021"

【问题讨论】:

    标签: python-3.x csv scrapy middleware


    【解决方案1】:

    对于所有遭受同样问题困扰的人:

    我刚刚在我的终端上点击:

    cat 输入文件 | tr -d '\0' > 输出文件.csv

    【讨论】:

      【解决方案2】:

      首先尝试更改vim中的编码:

      set fileencodings=utf-8
      

      或者在你的ubuntu机器上用不同的文本编辑器打开它,可能只是vim的问题。

      第二件事是下载正确名称的文件:

      import os
      from urllib.parse import unquote
      from scrapy.pipelines.files import FilesPipeline
      from scrapy.http import Request
      
      
      class TempPipeline():
          def process_item(self, item, spider):
              return item
      
      class ProcessPipeline(FilesPipeline):
          # Overridable Interface
          def get_media_requests(self, item, info):
              urls = ItemAdapter(item).get(self.files_urls_field, [])
              return [Request(u) for u in urls]
      
          def file_path(self, request, response=None, info=None, *, item=None):
          # return 'files/' + os.path.basename(urlparse(request.url).path) # from scrapy documentation
              return os.path.basename(unquote(request.url))           # this is what worked for my project, but maybe you'll want to add ".csv"
      

      你还需要更改 settings.py:

      ITEM_PIPELINES = {
          'myproject.pipelines.MyImagesPipeline': 300
      }
      
      FILES_STORE = '/path/to/valid/dir'
      

      尝试这两种方法,如果仍然不起作用,请更新我。

      【讨论】:

      • 感谢您的回答。我不需要图像管道 :( 我已经尝试在 vim 中解码,但它没有做任何更改
      • @y.y 如果你使用 'scrapy crawl -o file.csv -t csv' 会发生什么?
      【解决方案3】:

      我认为您的文件包含空字节。

      问题可能是:

      您的items.py 包含两个字段file_urlsfiles。但是,你的蜘蛛 yields 只有一项,即 file_urls。因此,CSV 使用两列(file_urlsfiles)创建,但文件列不包含任何数据(这可能会导致此问题)。尝试评论这一行,看看它是否有效#files = scrapy.Field()

      【讨论】:

      • 嘿 shivam,是的,它包含空字节。我尝试了你的建议,但没有任何改变
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2023-04-08
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多