淘寶: 》》 兩種方法
第一種 (selenium+火狐 +控制鼠標):
#! /uer/bin/evn python3
# -*- coding: utf-8 -*-
from selenium.webdriver import DesiredCapabilities
# from pyvirtualdisplay import Display
from selenium import webdriver
import time, random, re, json
# from random_ip import proxy_ip
# from random_ip_ua_xici import get_random_ip
from pynput.mouse import Button, Controller
from pynput.keyboard import Controller as KeyController
from pynput.keyboard import Key
from tb_save_usr import user_dict
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def get_s_cookie(account, password):
user_list = [
# user_dict
{'user': '%s' % account, 'password': '%s' % password}
]
proxy = Proxy(
{
# 'proxyType': ProxyType.MANUAL, # 用不用都行
'httpProxy': {'HTTPS': '183.159.84.172:18118'}
}
)
one_goods_url = 'https://login.taobao.com/member/login.jhtml?from=taobaoindex&f=top&style=&sub=true&redirect_url=https%3A%2F%2Fmyseller.taobao.com%2Fseller_admin.htm'
driver = webdriver.Firefox(proxy=proxy)
driver.get(one_goods_url)
driver.implicitly_wait(8)
# 讀鼠標座標
mouse = Controller()
# 設置鼠標座標
mouse.position = (1165, 320)
# 點擊鼠標
mouse.click(Button.left) # 點擊鼠標2下 mouse.click(Button.left, 2)
time.sleep(0.5)
mouse.release(Button.left)
time.sleep(random.uniform(1, 2))
k = KeyController()
# 清空輸入框
# for i in range(18):
# k.press(Key.backspace) # 按住鼠標左鍵
# k.release(Key.backspace) # 放開鼠標左鍵
mouse.position = (1038, 420)
mouse.click(Button.left) # 點擊鼠標2下 mouse.click(Button.left, 2)
time.sleep(0.5)
mouse.release(Button.left)
time.sleep(1)
user = random.choice(user_list)
# 輸入用戶名
for i in user['user']:
k.type(i)
time.sleep(random.uniform(0.5, 1.5))
k.press(Key.tab)
k.release(Key.tab)
time.sleep(1)
# 輸入密碼
for i in user['password']:
k.type(i)
time.sleep(random.uniform(0.5, 1.5))
# 鼠標移動到登錄按鈕位置
mouse.position = (1030, 540)
time.sleep(random.uniform(0.5, 1.5))
mouse.click(Button.left)
time.sleep(2)
time.sleep(30)
driver.get("https://mai.taobao.com/seller_admin.htm?")
# driver.get('https://subway.simba.taobao.com/')
# print('<>><>', driver.page_source)
cookie_items = driver.get_cookies()
# print('>>', cookie_items)
post = {}
for cookie_item in cookie_items:
post[cookie_item['name']] = cookie_item['value']
# print('--', post)
cookie_str = json.dumps(post)
# print('**', type(cookie_str), cookie_str)
driver.close()
with open('cookie_sycm.txt', 'w+', encoding='utf-8') as f:
# f.write(str(time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))))
f.write(cookie_str)
# page = driver.page_source
# print(page)
time.sleep(1)
if __name__ == "__main__":
get_s_cookie()
# get_cookie01()
第二種 (selenim+火狐 + 直接xpath元素)
#! /uer/bin/evn python3
# -*- coding: utf-8 -*-
from selenium.webdriver import DesiredCapabilities
# from pyvirtualdisplay import Display
from selenium import webdriver
import time, random, re, json
# from random_ip import proxy_ip
# from random_ip_ua_xici import get_random_ip
from retrying import retry
from pynput.mouse import Button, Controller
from pynput.keyboard import Controller as KeyController
from pynput.keyboard import Key
from tb_save_usr import user_dict
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
def get_s_cookie():
user_list = [
# user_dict
{'user': '17619', 'password': 'ly5@@'}
]
proxy = Proxy(
{
# 'proxyType': ProxyType.MANUAL, # 用不用都行
'httpProxy': {'HTTPS': '183.159.84.172:18118'}
}
)
post = {}
one_goods_url = 'https://login.taobao.com/member/login.jhtml?from=taobaoindex&f=top&style=&sub=true&redirect_url=https%3A%2F%2Fmyseller.taobao.com%2Fseller_admin.htm'
driver = webdriver.Firefox(proxy=proxy)
# time.sleep(random.uniform(1, 3))
driver.get(one_goods_url)
driver.implicitly_wait(8)
driver.find_element_by_xpath('//*[@id="J_Quick2Static"]').click()
time.sleep(random.uniform(0.5, 1.5))
# 輸入賬號密碼
user_data = random.choice(user_list)
for u in user_data['user']:
driver.find_element_by_xpath('//*[@id="TPL_username_1"]').send_keys('%s' % u)
time.sleep(random.uniform(0.2, 1.0))
time.sleep(random.uniform(0.8, 1.0))
for p in user_data['password']:
driver.find_element_by_xpath('//*[@id="TPL_password_1"]').send_keys('%s' % p)
time.sleep(random.uniform(0.2, 1.0))
time.sleep(random.uniform(0.5, 1.0))
driver.find_element_by_xpath('//*[@id="J_SubmitStatic"]').click()
# driver.get("https://mai.taobao.com/seller_admin.htm?")
# cookie_items = driver.get_cookies()
# for cookie_item in cookie_items:
# post[cookie_item['name']] = cookie_item['value']
# cookie_str = json.dumps(post)
# driver.close()
# with open('cookie_sycm.txt', 'w+', encoding='utf-8') as f:
# f.write(cookie_str)
# time.sleep(5)
if __name__ == "__main__":
get_s_cookie()
# get_cookie01()
#*****************************************************************************************************************************************************************************************************************************************************
京東
#! /uer/bin/evn python3
# -*- coding: utf-8 -*-
import requests, json, time, random
from random_ip_01 import proxy_ip_01
from random_UA_01 import random_ua_01
from random_UA import random_ua
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
from bs4 import BeautifulSoup
import urllib.parse
from lxml import etree
def get_cookie():
'''
獲取登錄cookie
'''
get_cookie01()
time.sleep(random.uniform(1, 3))
# display = Display(visible=0, size=(800, 600))
# display.start()
post = {}
one_goods_url = 'https://passport.jd.com/new/login.aspx?'
# ip = proxy_ip()['http']
chromedriver = '/usr/local/bin/chromedriver'
chromeOptions = webdriver.ChromeOptions()
# p_ip = '--proxy-server=http://' + proxy_ip()['http'] #'disable-infobars' "--proxy-server = http://140.250.152.160:42269"
# print(p_ip)
chromeOptions.add_argument("--proxy-server = http://140.250.152.160:42269")
driver = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
time.sleep(random.uniform(1, 3))
driver.get(one_goods_url)
driver.implicitly_wait(8)
driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()
driver.find_element_by_name('loginname').clear()
driver.find_element_by_name('loginname').send_keys('17611539')
driver.find_element_by_name('nloginpwd').clear()
driver.find_element_by_name('nloginpwd').send_keys('')
time.sleep(random.uniform(10, 15))
driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()
time.sleep(20)
driver.get("https://www.jd.com/")
cookie_items = driver.get_cookies()
# print('>>', cookie_items)
for cookie_item in cookie_items:
post[cookie_item['name']] = cookie_item['value']
# print('--', post)
cookie_str = json.dumps(post)
# print('**', type(cookie_str), cookie_str)
driver.close()
driver.quit()
with open('cookie02.txt', 'w+', encoding='utf-8') as f:
# f.write(str(time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))))
f.write(cookie_str)
# page = driver.page_source
# print(page)
if __name__ == "__main__":
get_cookie()