download chrome drive first base on the version you are using
https://chromedriver.chromium.org/downloads
# coding=utf-8
import os
from selenium import webdriver
import datetime
import time
from os import path
#此处chromedriver改为自己下载的路径
driver = webdriver.Chrome("C:\\Users\\Python\\Desktop\\chromedriver.exe")
#driver.maximize_window()
def login():
driver.get("https://www.taobao.com")
time.sleep(3)
if driver.find_element_by_link_text("登錄"):
driver.find_element_by_link_text("登錄").click()
print("请在15秒内完成扫码")
time.sleep(15)
driver.get("https://world.taobao.com/cart/")
time.sleep(3)
if driver.find_element_by_xpath("//div[@id='J_SelectAll1']//label"):
driver.find_element_by_xpath("//div[@id='J_SelectAll1']//label").click()
now = datetime.datetime.now()
print("login success:", now.strftime("%Y-%m-%d %H:%M:%S"))
def buy(buytime):
while True:
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(buytime)
print(now)
# 对比时间,时间到的话就点击结算
if now > buytime:
try:
if driver.find_element_by_xpath("//a[@id='J_Go']"):
#driver.find_element_by_id("J_Go").click()
#driver.find_element_by_link_text("提交订单").click()
driver.find_element_by_xpath("//a[@id='J_Go']").click()
driver.get("https://buy.taobao.com/auction/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined")
time.sleep(2)
driver.find_element_by_xpath("//a[@class='go-btn']")
time.sleep(1)
driver.find_element_by_xpath("//a[@class='go-btn']").click()
except:
time.sleep(0.1)
print(now)
time.sleep(0.1)
if __name__ == "__main__":
times = input("请输入抢购时间(例如格式:2018-11-11 00:00:00):")
login()
buy(times)
def get_cookie_from_network():
url_login = 'https://login.taobao.com/'
driver.get(url_login)
driver.find_element_by_css_selector('#fm-login-id').send_keys('xxxxxx') # 改成你的账号
driver.find_element_by_css_selector('#fm-login-password').send_keys('xxxxxxxx') # 改成你的密码
driver.find_element_by_tag_name('button').click() # 点击登录
# 获得 cookie信息
cookie_list = driver.get_cookies()
pickle.dump(cookie_list, open("cookies.pkl","wb"))
def load_cookie_from_file():
url_login = 'https://login.taobao.com/'
driver.get(url_login)
cookies = pickle.load(open("cookies.pkl", "rb"))
# print(cookies)
for cookie in cookies:
if "expiry" and 'name' and 'value' in cookie.keys():
exp_date = cookie['expiry']
if exp_date > time.time():
print(cookie)
else:
pass
driver.add_cookie(cookie)
def get_weibo_list(self, user_id):
import requests
from bs4 import BeautifulSoup as bs
cookdic = load_cookie_from_file()
headers = {
'User-Agent'
:
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'
}
timeout = 5
r = requests.get(url, headers=headers, cookies=cookdic,timeout=timeout)
soup = bs(r.text,
'lxml'
)
...
# 用BeautifulSoup 解析网页
import requests
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.zhihu.com/')
# 不考虑验证码的情况
driver.find_element_by_xpath('//button[@data-za-detail-view-id="2278"]').click() #点击登录进入登录界面
driver.find_element_by_xpath('//input[@name="username"]').send_keys('account') #发送帐号名
driver.find_element_by_xpath('//input[@name="password"]').send_keys('password',Keys.ENTER) #发送密码并回车
time.sleep(10) # 等待cookie加载完成
cookies = driver.get_cookies()
print(cookies)
s=requests.Session()
c = requests.cookies.RequestsCookieJar()
for item in cookies:
c.set(item["name"],item["value"])
print(c)
s.cookies.update(c) # 载入cookie