如何在Python + Selenium中使用Cookie抓取网站

时间:2018-09-20 05:35:16

标签: python-3.x selenium cookies http-headers payload

我使用以下代码登录网站,登录成功后,我想使用Cookie信息进行Selenium抓取,直到现在我都使用Selenium而没有使用cookie,我不确定该怎么办?

class login():
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.session = requests.Session()

        self.cookie = self.session.get("https://passport.site.com/v2/api/?getapi&class=login&tpl=mn&tangram=true", verify=False)
        self.token = re.findall(r"bdPass.api.params.login_token='(.*?)'", self.cookie.text)[0]

        self.headers = {
            'Host': 'passport.site.com',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601.1',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
            'Content-Type': 'application/x-www-form-urlencoded'
        }

       self.payload = {
            'charset': 'utf-8',
            'token': self.token,
            'username': self.username,
            'password': self.password,
            ...
        }

        self.login = self.session.post("https://passport.site.com/v2/api/?login", data=self.payload, headers=self.headers, verify=False)

0 个答案:

没有答案