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
1 from selenium import webdriver 2 import time 3 import string 4 import datetime 5 6 7 def usage(): 8 print("*********************************************************************") 9 print("欢迎使用Amazone差评神器,Enover保留版权,作者:Anker 日期:2016-12-18") 10 print("*********************************************************************") 11 12 def genSearchDate(): 13 now = datetime.datetime.now() 14 print("当前的日期是:%s/%s/%s" % (now.day, now.month, now.year%2000)) 15 16 #计算当前月的的日期范围 17 dayarr = [] 18 if now.day <= 10 : 19 dayarr = [10,1] 20 elif now.day/10 <= 2: 21 dayarr = [now.day,10,1] 22 else: 23 dayarr = [now.day,20,10,1] 24 25 #判断是否闰年 26 day2 = 0 27 if (now.year%4 == 0 and now.year%100 != 0) or now.year%400 == 0: 28 day2 = 29 29 else: 30 day2 = 28 31 32 33 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]] 34 35 mon=now.month 36 searchDate=[] 37 while (mon > 0): 38 if (mon == now.month): 39 tmp = dayarr 40 else: 41 tmp = months[mon] 42 for d in range(0,len(tmp)-1): 43 if d==0: 44 enddate='%s/%s/%s' % (mon, tmp[d], now.year%2000) 45 else: 46 enddate='%s/%s/%s' % (mon, tmp[d]-1, now.year%2000) 47 begdate='%s/%s/%s' % (mon, tmp[d+1], now.year%2000) 48 val=[begdate,enddate] 49 searchDate.append(val) 50 mon=mon-1 51 #print(searchDate) 52 return searchDate 53 54 55 #登陆亚马逊 56 def loginAmazone(driver): 57 driver.get("https://sellercentral.amazon.com") 58 driver.find_element_by_id('ap_email').send_keys('xxxxx') 59 driver.find_element_by_id('ap_password').send_keys('xxxxx') 60 driver.find_element_by_name('signIn').submit() 61 62 63 #设置查询条件 ASIN 和 时间 64 def searchProcess(driver, asin, begdate,enddate): 65 driver.get("https://sellercentral.amazon.com/gp/orders-v2/search/ref=ag_myosearch_apsearch_myo") 66 driver.find_element_by_id('_myoSO_searchTypeSelect').send_keys('ASIN') 67 driver.find_element_by_id('_myoSO_searchKeyword').send_keys(asin) 68 69 driver.find_element_by_id('_myoSO_SearchOption_exactDates').click() 70 driver.find_element_by_id('exactDateBegin').clear() 71 driver.find_element_by_id('exactDateBegin').send_keys(begdate) 72 driver.find_element_by_id('exactDateEnd').clear() 73 driver.find_element_by_id('exactDateEnd').send_keys(enddate) 74 75 driver.find_element_by_id('_myoSO_SearchButton').click() 76 time.sleep(2) 77 78 79 #设置每页显示50个 80 def setpage50(driver): 81 driver.find_element_by_xpath('//option [@value="50"]').click() # click 82 driver.find_element_by_xpath('//form [@onsubmit="return MYO.LO.DoAjaxSearchCall( this );"]').submit() 83 time.sleep(2) 84 driver.find_element_by_id('_myoLO_saveDefaultSearchCheckBox').click() 85 86 #计算记录个数 87 def countPage(source): 88 pattern='</strong> of <strong>' 89 pos1=source.find(pattern) 90 beg=pos1+len(pattern) 91 pos2=source.find('</strong>',pos1+len(pattern)) 92 total=int(source[beg:pos2]) 93 94 page=total%50 95 if page==0: 96 page=total/50 97 else: 98 page=int(total/50)+1 99 print("订单总数为:%s,共计%s页" % (total, page)) 100 return page 101 102 #翻页 jump to page 103 def jumppage(driver, page, custid): 104 rc=False 105 for index in range(1,page): 106 print("正在查找第%s页" % index) 107 elements = driver.find_elements_by_xpath('//input [@maxlength="7"]') 108 elements[1].find_element_by_xpath('//input [@name="currentPage"]').send_keys(str(index)) 109 driver.find_element_by_id('_myoSO_GoToPageForm_1').submit() 110 time.sleep(4) 111 source=driver.page_source 112 pos=source.find(custid) 113 if pos != -1: 114 print('终于找到了,查找记录如下:') 115 print(source[pos-270:pos+24]) 116 rc=True 117 break 118 return rc 119 120 def searchBadReview(driver, asin, custid, searchDate): 121 for i in range(0, len(searchDate)): 122 tmpDate=searchDate[i] 123 begdate=tmpDate[0] 124 enddate=tmpDate[1] 125 print('==============================================') 126 print("开始找%s到%s的订单" %(begdate, enddate)) 127 searchProcess(driver, asin, begdate, enddate) 128 setpage50(driver) 129 source=driver.page_source 130 page=countPage(source) 131 rc = jumppage(driver, page, custid) 132 if rc == True: 133 break 134 135 136 #主函数 137 def main(): 138 usage() 139 140 #输入参数 141 asin = input("请输入ASIN:") 142 print("你输入的ASIN是: ", asin) 143 custid = input("请输入Customer profile id:") 144 print("你输入的内容是: ", custid) 145 searchDate=genSearchDate() 146 #print("查找时间范围如下:") 147 #print(searchDate) 148 149 #默认浏览器行为 150 print('==============================================') 151 print("开始打开浏览器,并登陆Amazone seller center") 152 driver = webdriver.Chrome() 153 loginAmazone(driver) 154 time.sleep(1) 155 searchBadReview(driver, asin, custid, searchDate) 156 driver.quit() 157 time.sleep(60) 158 159 if __name__ == "__main__": 160 main()
引自:https://www.cnblogs.com/Anker/p/6195172.html