我已经设置了这个简单的网站,上面只有一个表单,所以我可以用Python来练习填写:http://www.wix.com/yoshyaro/status-check。
查看urllib2 - The Missing Manual,我的代码配置如下
url = 'http://www.wix.com/yoshyaro/status-check'
values = {'name':"my name",
'email':"yoshyaro@yahoo.com",
'subject':"Subject",
'Message':"Hello"}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
当我理解这个代码可以工作时,我们正在向服务器发送一个POST请求,其中包含请求中打包的所有表单数据。但是,我正在使用的方法必须做错,因为没有发生任何事情。我没有收到任何确认电子邮件。
因此,我手动填写表单,点击提交,然后查看Chrome的网络标签。我看到一个POST请求,其中包含以下信息。
Request URL:http://editor.wix.com/wixpress/sendEmail.jspx
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:886
Cookie:_wix_browser_sess=2086d943-1f2e-49e9-88d1-c09bbb514503; userType=REGISTERED; wixMobileEditor=0d42663f8a3da0153266b3162d390f57; s_session_id=0fb8f99e-41bf-4588-85bb-6428cf5ba16e; _wixRefX="www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cts=1330620782943&ved=0CIoBEBYwAQ&url=http%3A%2F%2Fwww.wix.com%2F&ei=Y6lPT-GjI-mIsgLH8om3Dg&usg=AFQjCNG4eaeAPBPCnLcG_xTc75FuiYPfOg"; _wixCampX=null-campaign; _wixExprX=null-expr; _wixLndX=10882255; _wixTSX=1330620778929; _wixUIDX=24872778|9ba62417-c8e2-4134-bb3f-2ff53b8e39f6; _wixCIDX=f2219c7f-a421-49b2-8995-056bda65232b; wixSession=4b32ea4c6cd8cc8a6d5862a1ebb4ab245fdece4824a83478d52d03d0842cea02f78e0fbc7b497e98b08d89d92df81fd9ce6db2d1818da1ee93040d1b16b84031dd4483931900f73620a618e08ef4d65a685dd50df21046a6ee9730c337d4c20cf3445b288473e1a8c7609294d51aa14484105ad2e28fbba9e8d8ac109bf0e6d6c04b1a396b84c585ca59f39a6105736eb2e0a148f0faf3c6824205836d0596c90947041173a061ba18f71734f338658996a52b6b0d02ccdd421066b794a70909efcf259c3575a7e2a0e8a66f8c5593f4161bcd40435f4c9cd7c13cec7efaa2d830915b1156d69bc468dd982ce3ca5344a0e916432608f3375de2a3eaea2917ba4382406432c4263e4171c46a539ffcaa; wixClient=yoshyaro|yoshyaro@yahoo.com|NOT_VERIFIED_OPT_IN|0|1330622652098|1331918652098|9ba62417-c8e2-4134-bb3f-2ff53b8e39f6; wixLanguage=en-us; firstEditorSession=0; _wixAB2=622#2572#2012-03-09T08-21-15.000-0600|596#2356#2012-04-30T10-14-02.000-0500; __utma=41622436.41813915.1327611501.1330620785.1330625106.4; __utmb=41622436.2.10.1330625106; __utmc=41622436; __utmz=41622436.1330625106.4.3.utmcsr=static.wix.com|utmccn=(referral)|utmcmd=referral|utmcct=/client/app.swf
Host:editor.wix.com
Origin:http://static.wix.com
Referer:http://static.wix.com/client/app.swf?cacheKiller=v%3D668&max-age=172800
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
content-type:application/x-www-form-urlencoded
Form Dataview URL encoded
orgDocID:23287851
partner_id:WMGs4POB1ko-a
icon_file_name:9ba624_cc7e5becb5470aeefad64d8bf0d58476.wix_doc_ico
gu_id:ff72209a-5149-42e1-9ab4-c48690db9e5e
user_action:send_simple_email
experiment_id:empty
document_name:status-check
from_name:Name
user_name:yoshyaro
to:yoshyaro@yahoo.com
wixComputerID:JPgBWueD99+7DhhlYULLRJXbWSBbsxqoMbgepccE+qfEleS0yIMcGfyt2Ave2q5U6Vmw+vQ64Xs5q7p5Z1lMxw==
from:yoshyaro@yahoo.com
document_id:23287851
content:Name has sent you a message using your contact form at: <a href='http://www.wix.com/yoshyaro/status-check/My Website'>http://www.wix.com/yoshyaro/status-check/My Website</a>
Senders email: yoshyaro@yahoo.com
message
Thank you for using WIX.COM
subject:subject
Response Headersview source
AppServer:app6.wixpress.com
Cache-Control:no-cache
Content-Encoding:gzip
Content-Length:125
Content-Type:text/xml;charset=UTF-8
Date:Thu, 01 Mar 2012 18:20:39 GMT
Expires:-1
Pragma:no-cache
Server:Apache
Vary:Accept-Encoding
X-Via-Node:sputnik3
我注意到的第一件事是请求URL与我设置的URL不同。所以我将我的代码更改为:
url = 'http://editor.wix.com/wixpress/sendEmail.jspx'
values = {'name':"my name",
'email':"yoshyaro@yahoo.com",
'subject':"Subject",
'Message':"Hello"}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
但是运行它会产生HTTP 403错误:禁止。现在假设我正在尝试做的就是以适当的方式重新创建HTTP请求,我开始再次查看“网络”选项卡以查看这样的请求到底是什么样的。我此时的计划是尝试手工重建。但是,这就是我被困住的地方。我没有看到任何与POST请求中的实际内容有关的信息。我看到输出是什么,“[name]已发送消息”在POST发布后显示,但我无法在任何地方找到任何实际的表单值。
所以我就是这样。我不确定如何重新创建POST请求,因为我不确定它甚至是什么样的..