Germey / CookiesPool

CookiesPool Based on Redis
153 stars 102 forks source link

求助 #1

Open hanfuyin opened 7 years ago

hanfuyin commented 7 years ago

小弟在微博搜索验证码自动识别的一个小例子里借用了大哥的代码。可是response = requests.get(url, cookies=cookies_dict)获取验证码这里,是附带cookie获取的,云打码也识别了。点击提交的时候总是提示验证码不对。为什么获取的验证码和当前用户关联不上呢。希望大哥给点儿思路

Germey commented 7 years ago

比如你在页面中看到的显示的验证码为1234,然后附带Cookies请求了一下验证码的图片,由于验证码的图片是请求一次变一次,所以当前你识别出来的验证码可能是改变的5678,和原先就是不一样了,只是识别的结果和当前你看到的结果不同。但是不影响识别,都加上Cookies直接提交就好了。

hanfuyin commented 7 years ago

我就是这样提交的,但是不知道为什么和当前用户就是关联不上。后来我用截屏的方式,可以了。 谢谢大哥的回复

hanfuyin commented 7 years ago

再请教一个问题 linux : centos7 server Chrome : 59.0.3071.109 做的是微博爬虫程序,因为频率限制会弹出验证码。经过多次试验以下代码在windows下没有问题。在linux下确不奏效,也没有返回任何报错信息。请问我这问题在哪里? 以下代码是在linux下执行的代码,windows下去掉Xvfb

vdisplay = Xvfb()
vdisplay.start()

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")

self.browser = webdriver.Chrome(chrome_options=chrome_options)
self.browser.set_window_size(1200, 900)
url = url
self.browser.get(url)

self.browser.delete_all_cookies()

cookieStr = eval(cookieStr)

for cookies in cookieStr:
    self.browser.add_cookie(eval('{"name":"'+cookies+'","value":"'+cookieStr[cookies]+'"}'))

self.browser.get(url)

wait = WebDriverWait(self.browser, 20)

try:
    backImg = str(uuid.uuid1()) + '.png'
    self.browser.save_screenshot(backImg)
    self.browser.get_screenshot_as_png()

    screenshot = Image.open(backImg)
    yzm = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.code_img img')))

    location  = yzm.location                     # 获取验证码x,y轴坐标
    size      = yzm.size                         # 获取验证码的长宽
    rangle    = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
    caotcha   = screenshot.crop(rangle)
    verifyImg = str(uuid.uuid1()) + '2.png'
    caotcha.save(verifyImg)

   result = self.ydm.identify(file=verifyImg)
    if not result:
        print('验证码识别失败, 跳过识别')
        return

    door = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.code_input .W_inputStp')))

    door.send_keys(result)

    submit = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.S_btn_b')))
    typeS = submit.click()

    os.remove(verifyImg)
    os.remove(backImg)

    self.browser.quit()
    vdisplay.stop()
except WebDriverException as e:
    print(e.args)