python,requests:使用post请求传递captcha

时间:2017-10-01 21:55:38

标签: python python-requests

我有以下要记录的网址列表:

base_url='https://misc.interactivebrokers.com/cstools/contract_info/index2.php?action=Details&site=GEN&conid='
conId=[280313912,289230167,285817885,289229956,256019341,289230102,289128542,289128563,289230371,289230337,287565355,285578089,287695374,285714991,287565358]
url_list=[base_url+str(x) for x in conId]

如果我尝试只获取其中一个网址的页面,它确实可以正常工作:

import requests
import lxml.html
s = requests.session()
page= s.get(url_list[0])
html = lxml.html.fromstring(page.text)
if html.xpath("//*[@id='contractSpecs']/table[5]/tr[2]/td[2]")!=[]:
    print("get_result")
else:
    print("missing")

但是,如果我尝试获取多个页面,该网站会引发一个问题:

for url in url_list:
    page= s.get(url)
    html = lxml.html.fromstring(page.text)
    if html.xpath("//*[@id='contractSpecs']/table[5]/tr[2]/td[2]")!=[]:
        print("get_result")
    else:
        print("missing")

在检查验证码结果后,我可以看到源页面如下:

<br>
 <form type="post">
  To continue please enter the text from the image below
  <br>
   <img src="image.php?str=1wVfP3">
    <!--img src="https://chatsrv1.interactivebrokers.com/cstools/contract_info/v3.8/image.php?str=1wVfP3"-->
    <br>
     Text:
     <input name="filter" type="text">
      <input name="action" type="hidden" value="Details"/>
      <input name="conid" type="hidden" value="280313912"/>
      <input name="contract_id" type="hidden" value=""/>
      <input name="noBanner" type="hidden" value=""/>
      <input name="rescnt" type="hidden" value="100"/>
      ..............
     </input>
    </br>
   </img>
  </br>
 </form>
</br>

验证码答案位于<img src="image.php?str=1wVfP3">标签中! (在str=之后)

我知道这是因为我过去有第三个人为我做了这个代码,但是他用一个名为&#34; Grab&#34;的模块做到了。我现在正试图使用​​请求重构该代码。

为了通过验证码,Grab中使用的其他编码器:

try:
    text = r.xpath_text('//form')
    standart_text = 'To continue please enter the text from the image below Text:'
    if text == standart_text:
        print('captcha detected')
        cupcha = r.xpath('//img').get('src')
        cupcha_text = str(cupcha[14:])
        r.doc.set_input('filter', cupcha_text)
        r.doc.submit()

The set_input method correspond to a post request.。似乎抓取可以理解什么是表单字段并自动填充它们。

我的问题是,只有在抓取时才显示验证码 - 这意味着我无法使用谷歌浏览器的检查&gt;网络来检查表单的归档名称。

所以当我在html var上运行以下内容(见下文)时,我仍然会收到验证码问题:

def captcha_test(html):
    try:
        text=html.xpath("//form")[0].text
        standart_text = '\nTo continue please enter the text from the image below\n'
        if text == standart_text:
            print('captcha detected')
            cupcha = html.xpath('//img')[0].attrib['src']
            cupcha_text = str(cupcha[14:])
            s.post(fut_links.loc[0,"url"],data={'filter':cupcha_text})
            page=s.get(fut_links.loc[0,"url"])
            html = lxml.html.fromstring(page.text)
    except:
        print("no captcha")
    return html

任何人都知道如何在我最初获得的验证码页面中正确检查表单的字段,以便正确地发布帖子,知道所有正确的字段名称和要填写的字段?

0 个答案:

没有答案