使用RoboBrowser在OKCupid上提交表单

时间:2014-11-30 07:47:26

标签: python form-submit robobrowser

我正在尝试编写执行以下操作的python代码:

1)登录OKCupid

2)转到用户问题页面

3)回答未回答的问题。

我正在使用RoboBrowser来做到这一点。我可以做步骤1),2)我可以得到我想提交的问题的形式,但是一旦我提交(使用RoboBrowser的提交)它似乎没有通过OKCupid(因为问题不是注册为我的个人资料)。

这是我的代码。请注意,My_RoboBrowser只允许我将False传递给RoboBrowser.open以获取验证参数。

class My_RoboBrowser(RoboBrowser):
    def __init__(self, auth=None, parser=None, headers=None, user_agent=None, history=True):
        RoboBrowser.__init__(self, parser=None, user_agent=None, history=True)

    def Open(self, vURL, vVerify=True):
        response = self.session.get(vURL, verify=vVerify)
         self._update_state(response)

browser = My_RoboBrowser()
urlL = 'https://okcupid.com/login'

browser.open(url)

form = browser.get_form(id='loginbox_form')

form['username'] = 'Username'
form['password'] = 'Password'
browser.submit_form(form)

urlQ = 'https://www.okcupid.com/profile/USER/questions?low=1'
browser.open(urlQ)

Question_Tag = browser.find_all(class_="not_answered")[0]

ID = Question_Tag.get('data-qid')

#Get the form to fill out
Form = browser.get_form(id='answer_'+str(ID))
Form['my_answer'].value = '1'
Form['their_answer'].value = ['1']
Form['importance'].value = '1'
browser.submit_form(Form)

另外,如果我在IPython笔记本中查看表单对象Form时有用,那就说:

<RoboForm my_answer=, their_answer=[], importance=>
提交之前

<RoboForm my_answer=1, their_answer=['1'], importance=1>

后。

最后,如果它有用,以下是我试图回答的一种表格的代码(通过检查元素获得)

<form id="answer_179268" name="answer_179268" class="answer_area okform initialized"> 
<div class="container my_answer">  
   <input id="my_answer_1_179268" name="my_answer" value="1" false="" type="radio"> 
   <label class="radio" for="my_answer_1_179268">
      <span class="icon"></span>
         Yes
      </label>  
      <input id="my_answer_2_179268" name="my_answer" value="2" false="" type="radio"></input> 
   <label class="radio" for="my_answer_2_179268">
      <span class="icon"></span>
      No
      </label>  
   </div> 
<div class="container acceptable_answers">  
   <div class="title"> 
      <p>Answer(s) you’ll accept</p> 
   </div>   
   <label class="checkbox acceptable_answer" for="their_answer_1_179268">
      <input id="their_answer_1_179268" class="acceptable_answer" name="their_answer" value="1" false="" type="checkbox"></input>
      <span class="icon"></span>
       Yes
       </label>   

<label class="checkbox acceptable_answer" for="their_answer_2_179268">
   <input id="their_answer_2_179268" class="acceptable_answer" name="their_answer" value="2" false="" type="checkbox"></input?
      <span class="icon"></span>
       No
      </label>    
<label class="checkbox irrelevant" for="their_answer_any_179268">
   <input id="their_answer_any_179268" class="irrelevant" name="their_answer" value="irrelevant" type="checkbox"></input>
      <span class="icon"></span>
      Any of the above
      </label> 
   </div> 
<div class="container importance"> 
   <div class="title"> 
      <p>Importance</p> 
   </div> 
<div class="importance_radios">  
   <input id="importance_179268_5" name="importance" value="5" false="" type="radio"></input>
   <label class="importance_5 radio" for="importance_179268_5" data-count="5">
      <span class="icon"></span> 
      <div class="bar"></div> 
      <span class="label"></span> 
   </label>  
      <input id="importance_179268_4" name="importance" value="4" false="" type="radio"></input>
   <label class="importance_4 radio" for="importance_179268_4" data-count="4">
      <span class="icon"></span> 
   <div class="bar"></div> 
      <span class="label">A little</span> 
   </label>  
      <input id="importance_179268_3" name="importance" value="3" false="" type="radio"></input>
   <label class="importance_3 radio" for="importance_179268_3" data-count="3">
      <span class="icon"></span> 
   <div class="bar"></div> 
      <span class="label">Somewhat</span> 
</label>  
   <input id="importance_179268_2" name="importance" value="2" false="" type="radio"> </input>
<label class="importance_2 radio" for="importance_179268_2" data-count="2">
      <span class="icon"></span> 
   <div class="bar"></div> 
      <span class="label"></span> 
</label>  
      <input id="importance_179268_1" name="importance" value="1" false="" type="radio"> </input>
   <label class="importance_1 radio" for="importance_179268_1" data-count="1">
      <span class="icon"></span> 
   <div class="bar"></div> 
      <span class="label">Very</span> 
</label>  
</div> 
   <div class="irrelevant_message"> 
   <span class="irrelevant_text">Irrelevant</span> 
      <span class="message_text">(Because you’ll accept any answer, this question is marked irrelevant)</span> 
   </div> 
</div> 
<div id="explanation_container_179268" class="container explanation"> 
   <div id="answer_179268_explanationContainer" class="inputcontainer textarea noresize empty">
      <textarea id="answer_179268_explanation" class="noresize" placeholder="Explain your answer (optional)" false=""></textarea>
    <span class="message empty" style="height: 0"></span>
   <div class="icon"></div>
</div> 
</div>  
<button id="submit_btn_179268" class="submit_btn flatbutton disabled small">Answer</button>
<button id="cancel_btn_179268" class="cancel_btn flatbutton silver small">Cancel</button> 
   <a class="skip_btn inner" href="javascript:void(0)" draggable="false">Skip question</a>  
   <div id="public_container_179268" class="answer_privately">  
   <label class="checkbox" for="private_179268">
   <input id="private_179268" false="" type="checkbox"></input>
      <span class="icon"></span> 
       <span class="text">Answer privately</span> 
   </label> 
   </div>  
</form>

2 个答案:

答案 0 :(得分:1)

我见过几个人无法提交b / c网站使用Javascript提交表单的情况。换句话说,他们可以使用Robobrowser或Mechanize登录并填写表单,但表单本身无法提交 - 因为提交依赖于JS。这可能是你的问题。如果是这种情况,您应该尝试使用Selenium

您可以验证是否正在使用JS在浏览器的检查员中提交。 <ctrl + shift + i / network / *clear* inspector's network panel before clicking submit / click submit / check type for your Post>

我认为这是我的验证方式,但这里的其他人比我更清楚。祝你好运!

答案 1 :(得分:0)

你应该看看https://github.com/IvanMalison/okcupyd。它允许您在不使用浏览器的情况下执行此操作。