【发布时间】: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^@ ^@l^@v^@s^@"^@;^@"^@4^@"^@;^@"^@1^@2^@"^@;^@"^@5^@0^@"^@;^@"^@3^@2^@,^@6^@1^@"^@;^@"^@2^@0^@6^@,^@0^@0^@0^@0^@0^@"^@;^@"^@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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