selenium是進行web自動化測試的一個工具,支持C,C++,Python,Java等語言,他能夠實現模擬手工操作瀏覽器,進行自動化,通過webdriver驅動瀏覽器操作,我使用的是chrome瀏覽器,下載chrome webdriver 放到python的安裝目錄。
參考連接:
https://pypi.python.org/pypi/selenium
http://selenium-python.readthedocs.io/api.html
http://www.cnblogs.com/fnng/p/3160606.html
from selenium import webdriver
import time
import string
import datetime
def usage():
print("*********************************************************************")
print("歡迎使用Amazone差評神器,Enover保留版權,作者:Anker 日期:2016-12-18")
print("*********************************************************************")
def genSearchDate():
now = datetime.datetime.now()
print("當前的日期是:%s/%s/%s" % (now.day, now.month, now.year%2000))
#計算當前月的的日期范圍
dayarr = []
if now.day <= 10 :
dayarr = [10,1]
elif now.day/10 <= 2:
dayarr = [now.day,10,1]
else:
dayarr = [now.day,20,10,1]
#判斷是否閏年
day2 = 0
if (now.year%4 == 0 and now.year%100 != 0) or now.year%400 == 0:
day2 = 29
else:
day2 = 28
months=[[0,0],[31,20,10,1,],[day2,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1]]
mon=now.month
searchDate=[]
while (mon > 0):
if (mon == now.month):
tmp = dayarr
else:
tmp = months[mon]
for d in range(0,len(tmp)-1):
if d==0:
enddate='%s/%s/%s' % (mon, tmp[d], now.year%2000)
else:
enddate='%s/%s/%s' % (mon, tmp[d]-1, now.year%2000)
begdate='%s/%s/%s' % (mon, tmp[d+1], now.year%2000)
val=[begdate,enddate]
searchDate.append(val)
mon=mon-1
#print(searchDate)
return searchDate
#登陸亞馬遜
def loginAmazone(driver):
driver.get("https://sellercentral.amazon.com")
driver.find_element_by_id('ap_email').send_keys('xxxxx')
driver.find_element_by_id('ap_password').send_keys('xxxxx')
driver.find_element_by_name('signIn').submit()
#設置查詢條件 ASIN 和 時間
def searchProcess(driver, asin, begdate,enddate):
driver.get("https://sellercentral.amazon.com/gp/orders-v2/search/ref=ag_myosearch_apsearch_myo")
driver.find_element_by_id('_myoSO_searchTypeSelect').send_keys('ASIN')
driver.find_element_by_id('_myoSO_searchKeyword').send_keys(asin)
driver.find_element_by_id('_myoSO_SearchOption_exactDates').click()
driver.find_element_by_id('exactDateBegin').clear()
driver.find_element_by_id('exactDateBegin').send_keys(begdate)
driver.find_element_by_id('exactDateEnd').clear()
driver.find_element_by_id('exactDateEnd').send_keys(enddate)
driver.find_element_by_id('_myoSO_SearchButton').click()
time.sleep(2)
#設置每頁顯示50個
def setpage50(driver):
driver.find_element_by_xpath('//option [@value="50"]').click() # click
driver.find_element_by_xpath('//form [@onsubmit="return MYO.LO.DoAjaxSearchCall( this );"]').submit()
time.sleep(2)
driver.find_element_by_id('_myoLO_saveDefaultSearchCheckBox').click()
#計算記錄個數
def countPage(source):
pattern='</strong> of <strong>'
pos1=source.find(pattern)
beg=pos1+len(pattern)
pos2=source.find('</strong>',pos1+len(pattern))
total=int(source[beg:pos2])
page=total%50
if page==0:
page=total/50
else:
page=int(total/50)+1
print("訂單總數為:%s,共計%s頁" % (total, page))
return page
#翻頁 jump to page
def jumppage(driver, page, custid):
rc=False
for index in range(1,page):
print("正在查找第%s頁" % index)
elements = driver.find_elements_by_xpath('//input [@maxlength="7"]')
elements[1].find_element_by_xpath('//input [@name="currentPage"]').send_keys(str(index))
driver.find_element_by_id('_myoSO_GoToPageForm_1').submit()
time.sleep(4)
source=driver.page_source
pos=source.find(custid)
if pos != -1:
print('終於找到了,查找記錄如下:')
print(source[pos-270:pos+24])
rc=True
break
return rc
def searchBadReview(driver, asin, custid, searchDate):
for i in range(0, len(searchDate)):
tmpDate=searchDate[i]
begdate=tmpDate[0]
enddate=tmpDate[1]
print('==============================================')
print("開始找%s到%s的訂單" %(begdate, enddate))
searchProcess(driver, asin, begdate, enddate)
setpage50(driver)
source=driver.page_source
page=countPage(source)
rc = jumppage(driver, page, custid)
if rc == True:
break
#主函數
def main():
usage()
#輸入參數
asin = input("請輸入ASIN:")
print("你輸入的ASIN是: ", asin)
custid = input("請輸入Customer profile id:")
print("你輸入的內容是: ", custid)
searchDate=genSearchDate()
#print("查找時間范圍如下:")
#print(searchDate)
#默認瀏覽器行為
print('==============================================')
print("開始打開瀏覽器,並登陸Amazone seller center")
driver = webdriver.Chrome()
loginAmazone(driver)
time.sleep(1)
searchBadReview(driver, asin, custid, searchDate)
driver.quit()
time.sleep(60)
if __name__ == "__main__":
main()
