天天看點

layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊

layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊

python爬蟲——用selenium爬取京東商品資訊

1.先附上效果圖(我偷懶隻爬了4頁)

layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊
layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊

2.京東的網址https://www.jd.com/

3.我這裡是不加載圖檔,加快爬取速度,也可以用Headless無彈窗模式

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.先找到搜尋框并用selenium模拟點選(這裡發現京東不需要登入就能看到商品資訊)

layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

5.進入了第一頁,先寫好翻頁的函數,需要滑動到底部才能加載後30個商品,總共有60個商品

layui擷取input資訊_python爬蟲—用selenium爬取京東商品資訊
def next_page(page_number):    try:        # 滑動到底部        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")        time.sleep(random.randint(1, 3))#設定随機延遲        button = wait.until(            EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_bottomPage > span.p-num > a.pn-next > em'))        )#翻頁按鈕        button.click()# 翻頁動作        wait.until(            EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#J_goodsList > ul > li:nth-child(30)"))        )#等到30個商品都加載出來        # 滑動到底部,加載出後三十個貨物資訊        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")        wait.until(            EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#J_goodsList > ul > li:nth-child(60)"))        )#等到60個商品都加載出來             wait.until(            EC.text_to_be_present_in_element((By.CSS_SELECTOR, "#J_bottomPage > span.p-num > a.curr"), str(page_number))        )# 判斷翻頁成功,高亮的按鈕數字與設定的頁碼一樣        html = browser.page_source#擷取網頁資訊        prase_html(html)#調用提取資料的函數    except TimeoutError:        return next_page(page_number)