京東淘寶商品信息爬蟲(python和selenium庫)
(一)網(wǎng)站HTML元素分析
京東網(wǎng)的搜索不需要登錄,淘寶網(wǎng)需要,本實驗代碼選擇京東網(wǎng),搜索“肉”后搜索結果中一個商品的源代碼如下

目標數(shù)據(jù)在class=gl-item下
(二)使用瀏覽器實例登錄并爬取商品數(shù)據(jù)
使用Selenium和WebDriver驅(qū)動程序自動登錄京東商城,搜索指定的商品,并將商品的標題、價格、鏈接和評論量保存
導入庫
import csv
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
創(chuàng)建WebDriver實例,選擇適合的瀏覽器驅(qū)動程序,例如webdriver.Edge()用于Edge瀏覽器。
driver = webdriver.Edge()# 設置引擎為Edge,真實地打開一個Edge瀏覽器
打開京東商城網(wǎng)址,并使用driver.find_element()方法定位到搜索框,并輸入待搜索的商品名稱,這回選擇“書包“,如果是淘寶網(wǎng)這一步回車后需要掃碼登錄
url = 'https://www.jd.com/'
driver.get(url)
want = input('請輸入需要查詢的商品\n')
input_search = driver.find_element(By.ID,'key')# 通過id=key定位到搜索框
# 在輸入框中輸入“書包”
input_search.send_keys(want)
# 模擬鍵盤回車Enter操作進行搜索,
input_search.send_keys(Keys.ENTER)
使用time.sleep()進行強制等待,確保頁面內(nèi)容加載完全。
sleep(3)
使用driver.find_elements()方法定位到商品列表,并依次提取商品的鏈接、標題名稱、價格和評價數(shù)量,保存到字典中。
data=[]
goods = driver.find_elements(By.CLASS_NAME,'gl-item')
for good in goods:
? ? # 獲取商品鏈接
? ? link = good.find_element(By.TAG_NAME,'a').get_attribute('href')
? ? # 獲取商品標題名稱
? ? title = good.find_element(By.CSS_SELECTOR,'.p-name em').text.replace('\n', '')
? ? # 獲取商品價格
? ? price = good.find_element(By.CSS_SELECTOR,'.p-price strong').text.replace('\n', '')
? ? # 獲取商品評價數(shù)量
? ? commit = good.find_element(By.CSS_SELECTOR,'.p-commit a').text
? ? good_data = {
? ? ? ? ? ? '商品標題':title,
? ? ? ? ? ? '商品價格':price,
? ? ? ? ? ? '商品鏈接':link,
? ? ? ? ? ? '評論量':commit
? ? ? ? }
? ? data.append(good_data)
(三)數(shù)據(jù)保存為文件
創(chuàng)建CSV文件,實現(xiàn)數(shù)據(jù)的持久化。
file = path.join('good.csv')
# 以追加寫入的方式將商品數(shù)據(jù)保存到文件中
header = ['商品標題', '商品價格', '商品鏈接', '評論量']
with open(file, 'a+', encoding='utf-8', newline='') as wf:
? ? f_csv = csv.DictWriter(wf, header)
? ? f_csv.writeheader()
? ? f_csv.writerows(data)
使用driver.quit()方法關閉瀏覽器。
driver.quit()
實驗結果(csv文件截圖):

完整代碼如下:
import csv
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from os import path
# 京東商城網(wǎng)址
url = 'https://www.jd.com/'
want = input('請輸入需要查詢的商品\n')
driver = webdriver.Edge()# 設置引擎為Chrome,真實地打開一個Chrome瀏覽器
driver.get(url)
driver.implicitly_wait(3)# 隱式等待,確保動態(tài)內(nèi)容節(jié)點被完全加載出來——時間感受不到
#driver.maximize_window()# 最大化瀏覽器窗口,主要是防止內(nèi)容被遮擋
input_search = driver.find_element(By.ID,'key')# 通過id=key定位到搜索框
# 在輸入框中輸入“口罩”
input_search.send_keys(want)
# 模擬鍵盤回車Enter操作進行搜索
input_search.send_keys(Keys.ENTER)
# 強制等待3秒
sleep(3)
data=[]
goods = driver.find_elements(By.CLASS_NAME,'gl-item')
for good in goods:
? ? # 獲取商品鏈接
? ? link = good.find_element(By.TAG_NAME,'a').get_attribute('href')
? ? # 獲取商品標題名稱
? ? title = good.find_element(By.CSS_SELECTOR,'.p-name em').text.replace('\n', '')
? ? # 獲取商品價格
? ? price = good.find_element(By.CSS_SELECTOR,'.p-price strong').text.replace('\n', '')
? ? # 獲取商品評價數(shù)量
? ? commit = good.find_element(By.CSS_SELECTOR,'.p-commit a').text
? ? good_data = {
? ? ? ? ? ? '商品標題':title,
? ? ? ? ? ? '商品價格':price,
? ? ? ? ? ? '商品鏈接':link,
? ? ? ? ? ? '評論量':commit
? ? ? ? }
? ? data.append(good_data)
file = path.join('good.csv')
# 以追加寫入的方式將商品數(shù)據(jù)保存到文件中
header = ['商品標題', '商品價格', '商品鏈接', '評論量']
with open(file, 'a+', encoding='utf-8', newline='') as wf:
? ? f_csv = csv.DictWriter(wf, header)
? ? f_csv.writeheader()
? ? f_csv.writerows(data)
driver.quit()
如果要爬取淘寶網(wǎng)則需重新分析網(wǎng)頁,請參照上述代碼補全,示例如下:
import requests
from bs4 import BeautifulSoup
import time
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
# 京東商城網(wǎng)址
url = 'https://www.jd.com/'
want = input('請輸入需要查詢的商品\n')
driver = webdriver.Edge()
# 設置引擎為Chrome,真實地打開一個Chrome瀏覽器
driver.get('https://www.taobao.com/')
# 隱式等待,確保動態(tài)內(nèi)容節(jié)點被完全加載出來——時間感受不到
driver.implicitly_wait(3)
# 最大化瀏覽器窗口,主要是防止內(nèi)容被遮擋
driver.maximize_window()
time.sleep(2)
# 等待網(wǎng)頁加載
driver.find_element(By.ID,'q').send_keys(want) ? ?
#找到搜索框標簽,傳入需要查詢的內(nèi)容
driver.find_element(By.CLASS_NAME,'tb-bg').click() ?
#點擊搜索按鈕