亞馬遜廣告點擊
阿新 • • 發佈:2017-09-20
-a 賬號 type n) util img erro method hub 1.需要實現功能:
(1)打開首頁
(2)登錄賬號
(3)搜索關鍵詞,區別廣告
(4)排除自己的asin和標題
(5)點擊別的asin的廣告,進入詳情
第一步:打開谷歌驅動器:
driver = util.make_driver(ip=ip, disable_javascript=True)
def make_driver(ip,disable_javascript=True):
options = webdriver.ChromeOptions()
參數配置見登錄頁面
driver = webdriver.Remote(SELENIUM_HUB_SERVER, options.to_capabilities())
driver.implicitly_wait(1)
driver.set_window_size(1920, 1080)
driver.set_page_load_timeout(120)
return driver
第二步:打開首頁:
self.go(‘https://{host}‘.format(host=self.host))
第三步:點擊登錄按鈕:
self.click(self.until(lambda d: self.driver.find_element_by_id(‘nav-link-accountList‘)))
第四步:檢查是否到登錄頁面:
assert self.is_element_exist(‘//*[@id="signInSubmit"]‘), ‘open login page error. can not open page.‘
self.until(lambda d: self.driver.find_element_by_id(‘signInSubmit‘))
from selenium.webdriver.support.ui import WebDriverWait
def until(self, method, timeout=115):
wait = webdriver.support.ui.WebDriverWait(self.driver, timeout)
return wait.until(method=method)
第五步:輸入登錄帳戶和密碼並且點擊登錄按鈕:
self.driver.find_element_by_id(‘ap_email‘).send_keys(kwargs[‘email‘])
self.driver.find_element_by_id(‘ap_password‘).send_keys(kwargs[‘password‘])
self.driver.find_element_by_id(‘signInSubmit‘).click()
第六步:輸入關鍵詞並且點擊搜索按鈕
self.until(lambda d: self.driver.find_element_by_id(‘twotabsearchtextbox‘)).send_keys(kwargs[‘keyword‘])
# step 2
# 點擊搜索 提交
self.click(
self.until(
lambda d: self.driver.find_element_by_xpath(‘//input[@type="submit"][@class="nav-input"]‘)))
第七步:獲取頁面列表的所有產品
products = self.until(
lambda d: self.driver.find_elements_by_xpath(‘//li[contains(@class,"s-result-item")]‘))
第八步:循環獲取的產品.:
for product in products:
try:
# 查找有標記的產品
product.find_element_by_xpath(‘.//img[@class="s-sponsored-info-icon"]‘)
#獲取產品asin
asin = product.get_attribute(‘data-asin‘)
#判斷產品是不是本商店的
if ((asin,) in kwargs[‘asins‘]):
continue
else:
# 找鏈接
clickproduct = product.find_element_by_xpath(
‘.//a[contains(@class,"s-access-detail-page s-color-twister-title-link")]‘)
link = clickproduct.get_attribute(‘href‘)
self.gonewtag(link)#打開新的標簽
link_asin_list.append(asin)#記錄點擊的asin
time.sleep(2)
continue
except:
continue
付:正則獲取頁面asin:
def get_url_asin(self,url):
urlre = re.compile(r‘%2Fdp%2F(.+?)%2Fref‘)
asin = re.findall(urlre, url)[0]
return asin
第九步:返回點擊的asin:
return link_asin_list
亞馬遜廣告點擊