用python写一段淘宝模拟登陆的代码
时间: 2023-12-17 08:21:56 浏览: 53
好的,以下是用 Python 编写的淘宝模拟登陆的示例代码:
```python
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 获取 cookies
def get_Taobao_cookies(username, password):
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
# 进入淘宝登录页面
driver.get('https://login.taobao.com/member/login.jhtml')
# 切换到账号密码登录
driver.find_element_by_css_selector('.login-switch').click()
driver.find_element_by_css_selector('#fm-login-id').send_keys(username)
driver.find_element_by_css_selector('#fm-login-password').send_keys(password)
# 等待用户手动输入验证码
input('请手动输入验证码后再按回车键...')
# 获取 cookies
cookies = driver.get_cookies()
cookies_dict = {}
for cookie in cookies:
cookies_dict[cookie['name']] = cookie['value']
# 关闭浏览器驱动
driver.quit()
return cookies_dict
# 使用 cookies 登录淘宝
def login_Taobao_with_cookies(cookies):
s = requests.Session()
s.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'})
s.cookies.update(cookies)
# 测试是否登录成功,这里以我的淘宝为例
r = s.get('https://i.taobao.com/my_taobao.htm')
if '我的淘宝' in r.text:
print('登录成功')
else:
print('登录失败')
# 测试示例,替换为自己的淘宝账号密码
if __name__ == '__main__':
username = '你的淘宝账号'
password = '你的淘宝密码'
cookies = get_Taobao_cookies(username, password)
login_Taobao_with_cookies(cookies)
```
希望能够帮到你!如果还有其他问题,欢迎继续提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)