Python爬蟲之定時搶購淘寶商品
?
?
import
time
from
selenium
import
webdriver
import
datetime
class
Spider:
def
__init__
(self, url):
self.
__base_url
=
url
self.
__headers
=
{
'
User-Agent
'
:
'
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
'
,
}
def
get_url(self):
global
driver
driver.get(self.
__base_url
)
def
login(self):
global
driver
if
driver.find_element_by_link_text(
"
親,請登錄
"
):
driver.find_element_by_link_text(
"
親,請登錄
"
).click()
print
(
"
請在30秒內掃描登陸
"
)
time.sleep(
30
)
driver.get(
"
https://cart.taobao.com/cart.htm
"
)
def
selectAll(self,method):
global
driver
#
打開購物車列表頁面
driver.get(
"
https://cart.taobao.com/cart.htm
"
)
time.sleep(
3
)
#
是否全選購物車
if
method ==
0:
while
True:
try
:
if
driver.find_element_by_id(
"
J_SelectAll1
"
):
driver.find_element_by_id(
"
J_SelectAll1
"
).click()
break
except
:
#
print(f"找不到購買按鈕")
pass
if
method ==1
:
print
(f
"
請手動勾選需要購買的商品
"
)
time.sleep(
30
)
def
buy(self,times):
global
driver
while
True:
now
=
datetime.datetime.now()
#
對比時間,時間到的話就點擊結算
if
now >
times:
#
點擊結算按鈕
while
True:
try
:
if
driver.find_element_by_link_text(
"
結 算
"
):
driver.find_element_by_link_text(
"
結 算
"
).click()
print
(f
"
結算成功,準備提交訂單
"
)
break
except
:
pass
#
點擊提交訂單按鈕
while
True:
try
:
if
driver.find_element_by_link_text(
'
提交訂單
'
):
driver.find_element_by_link_text(
'
提交訂單
'
).click()
print
(f
"
搶購成功,請盡快付款
"
)
except
:
#
print(f"再次嘗試提交訂單")
pass
time.sleep(
0.01
)
option
=
webdriver.ChromeOptions()
option.add_argument(
'
disable-infobars
'
)
driver
= webdriver.Chrome(chrome_options=
option)
spider
= Spider(
"
https://www.taobao.com
"
)
spider.get_url()
spider.login()
#
設置0為全選、1為手動
spider.selectAll(0)
#
設置搶購時間
set_time = datetime.datetime(2019,6,6,1,25
)
spider.buy(set_time)
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

