使用RoboBrows在OKCupid上提交表单

2024-03-28 21:14:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

1)登录OKCupid

2)转到用户问题页面

3)未回答的问题。在

我用机器人来做这个。我可以执行步骤1),2),我可以得到我想要提交的问题的形式,但是一旦我提交(使用RoboBrowser的submit),它似乎不会通过OKCupid(在我的个人资料中,问题没有注册为答案)。在

这是我的密码。注意我的机器人浏览器只允许我向机器人浏览器打开对于verify参数。在

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笔记本中查看表单对象表单时有帮助,它会说:

^{pr2}$

提交前和

<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>

Tags: answernamedivformidfalseinputvalue
2条回答

你应该看看https://github.com/IvanMalison/okcupyd。它可以让你不用浏览器就可以做到这一点。在

我见过几个例子,有人不能提交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>

我想我就是这样验证的,但这里的其他人会比我更清楚。祝你好运!在

相关问题 更多 >