python脚本根据cookies自动登录网站_python 解决,能让程序的cookies根据浏览器cookies而变,或者有其他的思路 ?...

以登录清博大数据举例,登录后需要把cookie存本地,每次跑selenium时候就用谷歌跑:

#清博账号登录以及保存cookie

class Qingbo():

url = 'http://www.gsdata.cn/member/login'

def __init__(self):

self.data_path = cookie_path

if not os.path.exists('cookie'):

os.makedirs(self.data_path)

cap = webdriver.DesiredCapabilities.PHANTOMJS

cap["phantomjs.page.settings.resourceTimeout"] = 1000

# self.driver = webdriver.PhantomJS(desired_capabilities=cap)

self.driver = webdriver.Chrome()

self.driver.set_page_load_timeout(30)

def __del__(self):

self.driver.quit()

def save_cookie_to_file(self):

cookies = self.driver.get_cookies()

# print cookies

with codecs.open(cookie_path, mode='w+', encoding='utf-8') as f:

for cookie in cookies:

line = json.dumps(cookie, ensure_ascii=False) + '\n'

f.write(line)

def retry_get(self, url):

for retry in range(3):

try:

self.driver.get(url)

break

except TimeoutException:

self.driver.execute_script('window.stop()')

def login_in(self):

# try:

self.retry_get(self.url)

is_code = self.driver.find_element_by_xpath('//*[@id="wxLogin"]/p')

print(is_code.text)

if u'微信扫一扫' in is_code.text:

user_log_button = self.driver.find_element_by_xpath('/html//div[contains(@class,"login-box")]/div[contains(@class,"login-body")]/a[@class="login-type current"]')

print(user_log_button)

user_log_button.click()

uin_input = self.driver.find_element_by_name('username')

uin_input.clear()

uin_input.send_keys(QINGBO_USERNAME)

pwd_input = self.driver.find_element_by_name("password")

pwd_input.clear()

pwd_input.send_keys(QINGBO_PASSWORD)

pwd_input.send_keys(Keys.ENTER)

self.save_cookie_to_file()

self.__del__()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值