示例#1
0
def get_cookie_from_weibo_cn(account, password):
    """ 获取一个账号的Cookie """
    try:
        browser = webdriver.PhantomJS(desired_capabilities=dcap)
        browser.get("https://weibo.cn/login/")
        time.sleep(1)

        failure = 0
        while "微博" in browser.title and failure < 5:
            failure += 1
            browser.save_screenshot("aa.png")
            username = browser.find_element_by_name("mobile")
            username.clear()
            username.send_keys(account)

            psd = browser.find_element_by_xpath('//input[@type="password"]')
            psd.clear()
            psd.send_keys(password)
            try:
                code = browser.find_element_by_name("code")
                code.clear()
                if IDENTIFY == 1:
                    code_txt = raw_input(
                        "请查看路径下新生成的aa.png,然后输入验证码:")  # 手动输入验证码
                else:
                    from PIL import Image
                    img = browser.find_element_by_xpath(
                        '//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                    x = img.location["x"]
                    y = img.location["y"]
                    im = Image.open("aa.png")
                    im.crop((x, y, 100 + x, y + 22)).save("ab.png")  # 剪切出验证码
                    code_txt = identify()  # 验证码打码平台识别
                code.send_keys(code_txt)
            except Exception as e:
                pass

            commit = browser.find_element_by_name("submit")
            commit.click()
            time.sleep(3)
            if "我的首页" not in browser.title:
                time.sleep(4)
            if '未激活微博' in browser.page_source:
                print('账号未开通微博')
                return {}

        cookie = {}
        if "我的首页" in browser.title:
            for elem in browser.get_cookies():
                cookie[elem["name"]] = elem["value"]
            logger.warning("Get Cookie Success!( Account:%s )" % account)
        return json.dumps(cookie)
    except Exception as e:
        logger.warning("Failed %s!" % account)
        return ""
    finally:
        try:
            browser.quit()
        except Exception as e:
            pass
示例#2
0
def getCookie(account, password):
    """ 获取一个账号的Cookie """
    #try:
    if True:
        browser = webdriver.PhantomJS(desired_capabilities=dcap)
        #browser.get("https://weibo.cn/login/")
        browser.get('https://passport.weibo.cn/signin/login')
        time.sleep(1)

        failure = 0
        while u'登录' in browser.title and failure < 5:
            failure += 1
            browser.save_screenshot("aa.png")
            #username = browser.find_element_by_name("mobile")
            username = browser.find_element_by_id("loginName")
            username.clear()
            username.send_keys(account)

            #psd = browser.find_element_by_xpath('//input[@type="password"]')
            try:
                psd = browser.find_element_by_id('loginPassword')
                psd.clear()
                psd.send_keys(password)
                code = browser.find_element_by_name("code")
                code.clear()
                if IDENTIFY == 1:
                    code_txt = raw_input(
                        u"请查看路径下新生成的aa.png,然后输入验证码:")  # 手动输入验证码
                else:
                    from PIL import Image
                    img = browser.find_element_by_xpath(
                        '//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                    x = img.location["x"]
                    y = img.location["y"]
                    im = Image.open("aa.png")
                    im.crop((x, y, 100 + x, y + 22)).save("ab.png")  # 剪切出验证码
                    code_txt = identify()  # 验证码打码平台识别
                code.send_keys(code_txt)
            except Exception, e:
                pass

            #commit = browser.find_element_by_name("submit")
            commit = browser.find_element_by_id("loginAction")
            commit.click()
            time.sleep(3)
            if u'随时随地发现新鲜事' not in browser.title:
                time.sleep(4)
            if u'未激活微博' in browser.page_source:
                print u'账号未开通微博'
                return {}

        cookie = {}
        if u'微博' in browser.title:
            for elem in browser.get_cookies():
                cookie[elem["name"]] = elem["value"]
            logger.warning("Get Cookie Success!( Account:%s )" % account)
        return json.dumps(cookie)
示例#3
0
def getCookie(account, password):
    try:
        browser = webdriver.PhantomJS(desired_capabilities=cap)
        browser.get("https://weibo.cn/login/")
        time.sleep(1)
        failure = 0
        # 尝试三次
        while "微博" in browser.title and failure < 3:
            print 'try again %d' % failure
            failure += 1
            # 找到用户名输入
            browser.save_screenshot("login.png")
            username = browser.find_element_by_name("mobile")
            username.clear()
            username.send_keys(account)
            # 找到密码输入 唯一定位用xpath
            psd = browser.find_element_by_xpath('//input[@type="password"]')
            psd.clear()
            psd.send_keys(password)
            try:
                code = browser.find_element_by_name("code")
                code.clear()
                if IDENTIFY == 1:
                    code_txt = raw_input("请查看路径下新生成的login.png,然后输入验证码:")
                else:
                    from PIL import Image
                    # 唯一定位验证码图片
                    img = browser.find_element_by_xpath(
                        '//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                    # Style:CSSStyleDeclaration
                    x = img.location["x"]
                    y = img.location["y"]
                    im = Image.open("login.png")
                    im.crop(
                        (x, y, 100 + x, y + 22)).save("yundama.png")  # 剪切出验证码
                    print 'yundama'
                    usefile('\n' + 'yundama')
                    code_txt = identify()  # 验证码打码平台识别
                code.send_keys(code_txt.decode())
            except Exception, e:
                pass

            commit = browser.find_element_by_name("submit")
            commit.click()
            time.sleep(5)

        cookie = {}
        if "我的首页" in browser.title:
            for elem in browser.get_cookies():
                cookie[elem["name"]] = elem["value"]
            strs = u"获取Cookie成功!( 账号:%s )" % account + '\n'
            print strs
            logger.warning(strs)
        #dict转化成str格式
        return json.dumps(cookie)
示例#4
0
def get_cookie_from_weibo_cn(account, password):
    """ 获取一个账号的Cookie """
    try:
        browser = webdriver.PhantomJS(desired_capabilities=dcap)
        browser.get("https://weibo.cn/login/")
        time.sleep(1)

        failure = 0
        while "微博" in browser.title and failure < 5:
            failure += 1
            browser.save_screenshot("aa.png")
            username = browser.find_element_by_name("mobile")
            username.clear()
            username.send_keys(account)

            psd = browser.find_element_by_xpath('//input[@type="password"]')
            psd.clear()
            psd.send_keys(password)
            try:
                code = browser.find_element_by_name("code")
                code.clear()
                if IDENTIFY == 1:
                    code_txt = raw_input("请查看路径下新生成的aa.png,然后输入验证码:")  # 手动输入验证码
                else:
                    from PIL import Image
                    img = browser.find_element_by_xpath('//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                    x = img.location["x"]
                    y = img.location["y"]
                    im = Image.open("aa.png")
                    im.crop((x, y, 100 + x, y + 22)).save("ab.png")  # 剪切出验证码
                    code_txt = identify()  # 验证码打码平台识别
                code.send_keys(code_txt)
            except Exception, e:
                pass

            commit = browser.find_element_by_name("submit")
            commit.click()
            time.sleep(3)
            if "我的首页" not in browser.title:
                time.sleep(4)
            if '未激活微博' in browser.page_source:
                print '账号未开通微博'
                return {}

        cookie = {}
        if "我的首页" in browser.title:
            for elem in browser.get_cookies():
                cookie[elem["name"]] = elem["value"]
            logger.warning("Get Cookie Success!( Account:%s )" % account)
        return json.dumps(cookie)
示例#5
0
def ab(xid):

    url = 'https://zhaoshang.tmall.com/maintaininfo/liangzhao.htm?xid={}'.format(
        str(xid))
    headers = {
        'Host':
        'zhaoshang.tmall.com',
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language':
        'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding':
        'gzip, deflate',
        'Referer':
        'https://semir.tmall.com/shop/view_shop.htm?',
        'Connection':
        'keep-alive',
        'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
    }
    m = requests.get(url, headers=headers, cookies=aa)

    yz = re.compile('id="J_CheckCode" src="(.*?)" />').findall(m.text)
    token = re.compile("type='hidden' value='(.*?)'>").findall(m.text)

    for i, j in zip(yz, token):
        #重点  验证url 需要去掉amp;,否则会重新刷新验证码
        yz_url = 'https:' + str(i).replace('amp;', '')
        print(yz_url)
        _tb_token_ = j
        header = {
            'Host':
            'pin.aliyun.com',
            'Accept':
            'image/png,image/*;q=0.8,*/*;q=0.5',
            'Accept-Language':
            'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding':
            'gzip, deflate',
            'Referer':
            'https://zhaoshang.tmall.com/maintaininfo/liangzhao.htm?&xid={}'.
            format(str(xid)),
            'Connection':
            'keep-alive',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
        }
        result = requests.get(yz_url, headers=header, cookies=aa)

        with open('yzm.jpg', 'wb') as f:
            f.write(result.content)
        im = Image.open(
            'D:\\任谦\python3\\python3整理目录\\PycharmProjects\\untitled4\\yzm.jpg')
        im.show()
        im.close()
        yzm = identify()  # 验证码打码平台识别
        time.sleep(random.uniform(1, 4))
        out_url = 'https://zhaoshang.tmall.com/maintaininfo/liangzhao.htm?_tb_token_={}&checkCode={}&xid={}'.format(
            str(_tb_token_), str(yzm), str(xid))
        print(out_url)
        hd = {
            'Host':
            'zhaoshang.tmall.com',
            'Accept':
            'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language':
            'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding':
            'gzip, deflate',
            #重点,漏了就一直循环验证码
            'Referer':
            'https://zhaoshang.tmall.com/maintaininfo/liangzhao.htm?_tb_token_={}&checkCode=&xid={}'
            .format(str(_tb_token_), str(xid)),
            'Connection':
            'Keep-Alive',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
        }

        out = requests.get(out_url, headers=hd, cookies=aa)

        aaa = re.compile('src="data:image/png;base64,(.*?)" height=',
                         re.S).findall(out.text)

        for i in aaa:
            return i

        if '看不清' in out.text:
            return ab(xid)
示例#6
0
def getCookies(weibo):
    """ 获取Cookies """
    cookies = []
    loginURL = 'https://weibo.cn/login/'
    for elem in weibo:
        account = elem['no']
        password = elem['psw']
        try:
            browser = webdriver.PhantomJS(desired_capabilities=dcap)
            browser.get(loginURL)
            time.sleep(1)

            failure = 0
            while "微博" in browser.title and failure < 5:
                failure += 1
                browser.save_screenshot("aa.png")
                username = browser.find_element_by_name("mobile")
                username.clear()
                username.send_keys(account)

                psd = browser.find_element_by_xpath(
                    '//input[@type="password"]')
                psd.clear()
                psd.send_keys(password)
                try:
                    code = browser.find_element_by_name("code")
                    code.clear()
                    if IDENTIFY == 1:
                        code_txt = raw_input(
                            "请查看路径下新生成的aa.png,然后输入验证码:")  # 手动输入验证码
                    else:
                        from PIL import Image
                        img = browser.find_element_by_xpath(
                            '//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                        x = img.location["x"]
                        y = img.location["y"]
                        im = Image.open("aa.png")
                        im.crop(
                            (x, y, 100 + x, y + 22)).save("ab.png")  # 剪切出验证码
                        code_txt = identify()  # 验证码打码平台识别
                    code.send_keys(code_txt)
                except Exception, e:
                    pass

                commit = browser.find_element_by_name("submit")
                commit.click()
                time.sleep(3)
                if "我的首页" not in browser.title:
                    time.sleep(4)
                if '未激活微博' in browser.page_source:
                    print '账号未开通微博'
                    return {}

            cookie = {}
            if "我的首页" in browser.title:
                for elem in browser.get_cookies():
                    cookie[elem["name"]] = elem["value"]
                if len(cookie) > 0:
                    logger.warning("Get Cookie Successful: %s" % account)
                    cookies.append(cookie)
                    continue
            logger.warning("Get Cookie Failed: %s!" % account)
        except Exception, e:
            logger.warning("Failed %s!" % account)
示例#7
0
def getCookie(account, password, spider):
    """ 获取一个账号的Cookie """
    dcap = dict(DesiredCapabilities.PHANTOMJS
                )  # PhantomJS需要使用老版手机的user-agent,不然验证码会无法通过
    dcap["phantomjs.page.settings.userAgent"] = (
        "Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
    )
    # dcap["phantomjs.page.settings.userAgent"] = BaseHelper.random_user_agent()
    browser = webdriver.PhantomJS(desired_capabilities=dcap)
    try:
        # browser.set_window_size(480, 320)
        browser.get("https://weibo.cn/login/")
        time.sleep(3)
        import os
        try:
            os.remove("aa.png")
        except OSError:
            pass
        browser.save_screenshot("aa.png")

        failure = 0
        while "微博" in browser.title and failure < 5:
            failure += 1
            username = browser.find_element_by_id("loginName")
            username.clear()
            username.send_keys(account)

            psd = browser.find_element_by_id("loginPassword")
            psd.clear()
            psd.send_keys(password)
            try:
                code = browser.find_element_by_id("loginVCode")
                code.clear()
                if IDENTIFY == 1:
                    try:
                        from PIL import Image
                        im = Image.open("aa.png")
                        im.show()
                        im.close()
                    except:
                        print(u"请到当前目录下,找到验证码后输入")
                    code_txt = raw_input(
                        "请查看路径下新生成的aa.png,然后输入验证码:")  # 手动输入验证码
                else:
                    from PIL import Image
                    img = browser.find_element_by_xpath(
                        '//form[@method="post"]/div/img[@alt="请打开图片显示"]')
                    x = img.location["x"]
                    y = img.location["y"]
                    im = Image.open("aa.png")
                    im.crop(
                        (x, y, x + img.size.get("width"),
                         y + img.size.get("height"))).save("ab.png")  # 剪切出验证码
                    code_txt = identify()  # 验证码打码平台识别
                code.send_keys(code_txt)
            except Exception, e:
                pass

            commit = browser.find_element_by_id("loginAction")
            commit.click()
            time.sleep(3)
            time.sleep(4)
            # if "我的首页" not in browser.title:
            #     time.sleep(4)
            # if '未激活微博' in browser.page_source:
            #     print '账号未开通微博'
            #     return {}

        cookie = {}
        # if "我的首页" in browser.title or True:
        if browser.title is not None:
            for elem in browser.get_cookies():
                cookie[elem["name"]] = elem["value"]
                spider.logger.warning("Get Cookie Success!( Account:%s )" %
                                      account)
        return json.dumps(cookie)