Scrapy可以處理文件上傳通過以下方式:
from scrapy.http import FormRequest
formdata = {
'file': open('path/to/file', 'rb')
}
yield FormRequest(url=url, formdata=formdata, callback=self.parse_result)
from scrapy.http import Request
with open('path/to/file', 'rb') as file:
body = file.read()
headers = {
'Content-Type': 'multipart/form-data'
}
yield Request(url=url, method='POST', headers=headers, body=body, callback=self.parse_result)
yield FormRequest.from_response(response, formdata={'file': open('path/to/file', 'rb')}, callback=self.parse_result)