Scrapy用转义字符更改multipart / form-data中的某些字符

时间:2019-03-30 08:55:11

标签: python-3.x forms validation scrapy

在Chrome开发工具中,表单数据如下所示:

------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="hostURL"

http://oris.co.palm-beach.fl.us/or_web1/
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="pdfPath"

\\wcp01zfs-03.clerk.local\files2\ORISPDF\
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="pdfURL"

http://oris.co.palm-beach.fl.us/pdf/
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="pages"

1
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="id"

22591587
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="mpages"

1
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="doc_id"

22591587
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="page1"

image_from_file.asp?imageurl=\\ors_fs\ORImage\O\30338\O.30338.0268.0001.tif
------WebKitFormBoundarysGRfL8zMeuSs4zsH
Content-Disposition: form-data; name="WaterMarkText"

1
------WebKitFormBoundarysGRfL8zMeuSs4zsH--

当我将其复制到scrapy shell调试请求时,某些字符看起来有所不同,结果(我想)请求返回错误。 这是它看起来很scrap的样子(python 3)。

enter image description here

如何在Scrapy请求中正确使用use formdata?

这是我的Scrapy代码摘录:

def get_image(self, response):
        # inspect_response(response, self)
        url = 'http://oris.co.palm-beach.fl.us:8080/PdfServlet/PdfServlet27'
        headers = {
                            'origin': "http://oris.co.palm-beach.fl.us",
                            'upgrade-insecure-requests': "1",
                            'dnt': "1",
                            'content-type': "multipart/form-data; boundary=----WebKitFormBoundarysGRfL8zMeuSs4zsH",
                            'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                            'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                            'cache-control': "no-cache",
                             }
        id = response.xpath("//input[@name='doc_id']/@value").extract_first()                     

        body = '''------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="hostURL"

                    http://oris.co.palm-beach.fl.us/or_web1/
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="pdfPath"

                    \\wcp01zfs-03.clerk.local\files2\ORISPDF\
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="pdfURL"

                    http://oris.co.palm-beach.fl.us/pdf/
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="pages"

                    1
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="id"

                    {}
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="mpages"

                    1
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="doc_id"

                    {}
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="page1"

                    image_from_file.asp?imageurl=\\ors_fs\ORImage\O\30338\O.30338.0268.0001.tif
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH
                    Content-Disposition: form-data; name="WaterMarkText"

                    1
                    ------WebKitFormBoundarysGRfL8zMeuSs4zsH--'''.format(id, id)
        r =  scrapy.Request(url, method="POST", headers = headers, body = body, callback = self.get_pdf, dont_filter = True)
        yield r

这是我尝试模仿的形式。

<td valign="bottom">

                <form name="courtform" action="http://oris.co.palm-beach.fl.us:8080/PdfServlet/PdfServlet27" method="post" enctype="multipart/form-data">

        <input type="hidden" name="hostURL" value="http://oris.co.palm-beach.fl.us/or_web1/" size="60">
        <input type="hidden" name="pdfPath" value="\\wcp01zfs-03.clerk.local\files2\ORISPDF\" size="60">
        <input type="hidden" name="pdfURL" value="http://oris.co.palm-beach.fl.us/pdf/" size="60">

        <input type="hidden" name="pages" value="1" size="60">
        <!--<input type="hidden" name="pages" value="1" size="60">-->
        <input type="hidden" name="id" value="22590889" size="60">
        <input type="hidden" name="mpages" value="1" size="60">
        <input type="hidden" name="doc_id" value="22590889" size="60">

                <input type="hidden" name="page1" value="image_from_file.asp?imageurl=\\ors_fs\ORImage\O\30336\O.30336.1200.0001.tif" size="60">

        <input type="hidden" name="WaterMarkText" value="1" size="60">

            &nbsp;&nbsp;<input name="button" type="button" value="View PDF" onclick="javascript:ValidateAndSubmit(this.form)">&nbsp;&nbsp;

</form></td>

1 个答案:

答案 0 :(得分:0)

字符问题归因于转义符号。我必须转义转义符号,并且Formdata现在看起来相同。请求仍然不起作用。所以我想,需要使用更新的数据对此提出另一个问题。