amazon爬虫代码
def amazon(url):
driver.get(url)
time.sleep(2)
title=driver.find_element_by_xpath("//*[@id='cm_cr-product_info']").find_element_by_tag_name("h1").get_attribute("textContent").replace("\n","")
time.sleep(2)
author=driver.find_element_by_xpath("//*[@id='cr-arp-byline']").find_element_by_tag_name("a").get_attribute("textContent").replace("\n","")
time.sleep(2)
comment_text=title+','+author
start_flag = True
while True:
reviews=driver.find_element_by_xpath("//*[@id='cm_cr-review_list']").find_elements_by_css_selector(".a-section.review.aok-relative")
time.sleep(2)
for review in reviews:
name=review.find_element_by_tag_name("span").get_attribute("textContent").replace("\n","").replace(",",".")
time.sleep(2)
score=review.find_element_by_class_name("a-icon-alt").get_attribute("textContent")[0:6]
time.sleep(2)
comment=review.find_element_by_css_selector(".a-size-base.review-text.review-text-content").get_attribute("textContent").replace("\n","").replace(",",".").strip()
time.sleep(2)
comment_text=comment_text+'\n'+name+','+score+','+comment
try:
next_page=driver.find_element_by_class_name("a-last").find_element_by_tag_name("a").get_attribute("href")
time.sleep(2)
driver.get(next_page)
time.sleep(2)
except:
break
with open('/Users/jiajiazhou/Downloads/comments_amazon.csv','a',encoding="utf-8_sig") as f:
f.write('\n')
f.write('\n')
f.write(comment_text)
f.close()