问题
I am making a form request to a website using scrapy. The form requires to upload a pdf file, How can we do it in Scrapy. I am trying this like -
FormRequest(url,callback=self.parseSearchResponse,method="POST",formdata={'filename':'abc.xyz','file':'path to file/abc.xyz'})
回答1:
At this very moment Scrapy has no built-in support for uploading files.
File uploading via forms in HTTP was specified in RFC1867. According to the spec, an HTTP request with Content-Type: multipart/form-data
is required (in your code it would be application/x-www-form-urlencoded
).
To achieve file uploading with Scrapy, you would need to:
- Get familiar with the basic concepts of HTTP file uploading.
- Start with
scrapy.Request
(instead ofFormRequest
). - Give it a proper
Content-Type
header value. - Build the request body yourself.
See also: How does HTTP file upload work?
来源:https://stackoverflow.com/questions/39303851/scrapy-upload-file