python - selenium 可以使用 requests 包请求后的session对象 继续打开别的url吗?
阿神
阿神 2017-04-17 17:35:31
0
1
476

问题描述如下:

1.首先 通过 requests 提交 form 请求,登录成功后 保存 session

  1. 此时 可以通过selenium 用 登录之后的session 继续发送请求吗 ?因为想要实现自动回复消息功能,但是聊天窗口界面在别的 url中,而且 在聊天窗口界面需要通过 按enter键 或者 点击发送按钮发送消息

在这种情况下, selenium 和 requests 可以配合使用吗?谢谢各位了!

阿神
阿神

闭关修行中......

reply all(1)
洪涛

I have done similar functions, but I only used Selenium to do it. The core code is as follows:

def get_allgroupid():
    driver = webdriver.PhantomJS()
    driver.get(address)
    driver.add_cookie({
          'name': 'Cookie',
          'value':'p_uin=o ......此处省略若干字.....'
        })
    driver.get(address)
    jsonstr= unicode(driver.find_element_by_xpath("html/body").text)
    driver.quit()
    groupid_list=re.findall('\d*\d{4,}',jsonstr)
    return groupid_list

I thought about it again, and the answer to the question raised by the questioner is yes. You can do this:

  1. Use requests to submit a request and get the session after the request;

  2. Use Selenium to add the session in step 1 to the driver

  3. Use the driver to do what you want. At this time, the driver already contains the session.
    You try it and see if this idea works.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template