from selenium.webdriver import Chrome
# from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
import csv
opt = Options()
opt.add_argument(‘—headless’)
opt.add_argument(‘—disable-gpu’)
driver = Chrome(options=opt)
time.sleep(3)
driver.get(‘https://search.jd.com/Search?keyword=%E9%BB%91%E4%B8%9D&enc=utf-8&wq=%E9%BB%91%E4%B8%9D&pvid=657b0638977e4f899bd4e339e3834dc7‘)
time.sleep(3)
with open(‘selenium京东黑丝.csv’, ‘a’, encoding=’utf-8’, newline=’’) as f:
csv_writer = csv.writer(f)
csv_writer.writerow([‘店铺名称’, “名称”, “价格”, “店铺连接”, “图片链接”])
for i in range(5):
# 以下实现页面滑动 整个页面到一万元素 一次点击1000 然后点击下一页循环五次 用来实现五页的抓取
def drop_down():
driver.execute_script(‘window.scrollTo(0,1000)’)
time.sleep(3)
driver.execute_script(‘window.scrollTo(0,2000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,3000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,4000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,5000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,6000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,7000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,8000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,9000)’)
time.sleep(1)
driver.execute_script(‘window.scrollTo(0,10000)’)
time.sleep(1)
drop_down()
li_list = driver.find_elements_by_xpath('//*[@id="J_goodsList"]/ul/li')<br /> time.sleep(3)
# 找到输入框,输入python ===> 输入回车/点击搜索<br /> # web.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)
for li in li_list:<br /> dianputitle = li.find_element_by_xpath('./div/div[7]/span/a').text<br /> title = li.find_element_by_xpath('./div/div[4]/a/em').text.strip('京东超市').strip('爱心东东').strip('\n')<br /> jiage = li.find_element_by_xpath('./div/div[3]/strong/i').text<br /> dianpu_dizhi = li.find_element_by_xpath('./div/div[1]/a').get_attribute('href')<br /> img_dizhi = li.find_element_by_xpath('./div/div[1]/a/img').get_attribute('src')<br /> with open('selenium京东黑丝.csv', 'a', encoding='utf-8', newline='') as f:<br /> csv_writer = csv.writer(f)<br /> csv_writer.writerow([dianputitle, title, jiage, dianpu_dizhi,img_dizhi])<br /> time.sleep(0.5)<br /> print(title+"下载完成")<br /> driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[1]/a[9]/em').click()<br /> time.sleep(2)<br />driver.quit()