为在OKcupid上随机回答问题而编写的函数 answer_questions 。我把它写成pyokc
中 User 类的扩展名def answer_questions(self):
"""
Submit random answers to questions lying unanswered for
given User. Takes the number of questions to be answered
as argument
"""
get_questions = self._session.post('http://www.okcupid.com/profile/{0}/questions'.format(self.username))
tree = html.fromstring(get_questions.content.decode('utf8'))
new_question = tree.xpath("//*[@id='new_question']")[0]
# question ID as well as targetid (associated with each User )
qid = int(tree.xpath("//div[contains(@id,'qtext_')]/@id")[0].split("_")[1])
targetid = int(tree.xpath("//input[@id='target_userid']/@value")[0])
number_of_answers = len(new_question.xpath(".//input[contains(@id,'my_answer_')]/@id"))
number_of_acceptable_answers = len(new_question.xpath(".//input[contains(@id,'their_answer_')]/@id"))
request_parameters = {
'ajax': 1,
'submit': 1,
'answer_question' : 1,
'skip' : 0,
'show_all' : 0,
'targetid' : targetid,
'qid' : qid,
'is_new' :1,
'answers' : random.randint(1, number_of_answers),
'matchanswers' : random.randint(1,number_of_acceptable_answers),
'importance' : 4,
'is_public': 1,
'note' : '',
'delete_note' : 0,
}
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.74 Safari/537.36',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
}
profiles_request = self._session.post('http://www.okcupid.com/profile/{0}/questions/'.format(self.username), data=request_parameters, headers=headers )
print (profiles_request)
POST请求返回响应200,但问题在我的个人资料中仍未显示,即服务器状态没有变化。
对于我直接在okcupid上回答的问题,fiddler所示的标题和表单数据如下: -
常规
Remote Address:198.41.209.133:80
Request URL:http://www.okcupid.com/questions/ask
Request Method:POST
Status Code:200 OK
响应标题
Cache-control:private
CF-RAY:1e7ea9cb7f7d0bb7-HKG
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Sun, 17 May 2015 10:33:18 GMT
P3P:CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT", policyref="http://www.okcupid.com/w3c/p3p.xml"
Server:cloudflare-nginx
Transfer-Encoding:chunked
X-OKWS-Version:OKWS/3.1.33.0
X-XSS-Protection:1; mode=block
请求标题
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:168
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:__cfduid=d8678fe61e59e41eb20e760ae5c70a0f71430320840; authlink=5cbd8d4f6953a34e; signup_exp_2014_09_13=2014_simpleblue; override_session=0; session=123132212344212%3a6473645976835253592; nano=k%3Diframe_prefix_lock_1%2Ce%3D1431858786856%2Cv%3D1%7Ck%3Diframe_prefix_lock_2%2Ce%3D1431858856830%2Cv%3D1
Host:www.okcupid.com
Origin:http://www.okcupid.com
Referer:http://www.okcupid.com/questions
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.74 Safari/537.36
X-Requested-With:XMLHttpRequest
表单数据
ajax:1
submit:1
answer_question:1
skip:0
show_all:0
targetid:123132212344212
qid:2209
is_new:1
answers:2
matchanswers:2
importance:3
is_public:1
note:
delete_note:0
你有什么建议/想法我的代码出错吗?