### 1.說明
利用selenium抓取淘寶商品并用PyQuery解析得到商品的圖片、名稱、價格、購買人數、店鋪名稱、店鋪所在地信息,將其保存在MongoDB
### 2.準備
[安裝selenium](/1kai-fa-huan-jing-pei-zhi/12-qing-qiu-ku-de-an-zhuang/122-seleniumde-an-zhuang.md)
[安裝ChromeDriver](/1kai-fa-huan-jing-pei-zhi/12-qing-qiu-ku-de-an-zhuang/123-chromedriverde-an-zhuang.md)
### 3.接口分析
### 
### 4.頁面數據分析
目的爬取商品信息

商品基本信息:商品圖片、名稱、價格、購買人數、店鋪名稱、店鋪所在地
抓取頁面:[https://s.taobao.com/search?q=蘋果plus正品](https://s.taobao.com/search?q=蘋果plus正品)
分頁:
### 5.獲取商品列表
抓取地址:[https://s.taobao.com/search?q=蘋果plus正品](https://s.taobao.com/search?q=蘋果plus正品)
```
def get_products():
"""
提取商品數據
"""
print("數據提取中....")
html = browser.page_source
doc = pq(html)
items = doc('#mainsrp-itemlist .items .item').items()
for item in items:
image = item.find(".pic-link .img").attr('data-src')
# if image:
# result = re.match('.*?!!(.*?)\..*?', image)
# result = re.sub('[a-z\_\-]', '', result)
# if result:
# id = result.group(1)
# print(id)
data = {
'title': item.find(".title").text().strip(),
'image':image,
'price':item.find(".price").text().strip(),
'deal':item.find('.deal-cnt').text().strip(),
'shop':item.find('.shop').text().strip(),
'location':item.find(".location").text().strip(),
}
print(data)
save_to_mongo(data)
```
### 6.存儲數據到MongoDB中
```
MONGO_URL = "localhost"
MONGO_DB = "taobao"
MONGO_COLLECTION = "products"
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
collection = db[MONGO_COLLECTION]
def save_to_mongo(data):
"""
保存數據到mongoDB中
:param data:
:return:
"""
try:
if collection.insert(data):
print("success")
except:
print("fail")
```
### 7.遍歷每頁
```
def main():
for i in range(1,101):
index_page(i)
# browser.close()
```
### 8.Chrome Headless模式
chrome無界面模式參數
```
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
```
### 9.對接Firefox
```
browser = webdriver.Firefox()
```
### 10.對接PhantomJS
```
browser = webdriver.PhantomJS()
```
設置緩存和禁用圖片加載的功能,進一步提高爬取效率
```
SERVICE_ARGS = ['--load-images=false', '--disk-cache=true']
browser = webdriver.PhantomJS(service_args=SERVICE_ARGS)
```
### 11.源代碼
```
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from pyquery import PyQuery as pq
import re
import pymongo
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
keyword = ""
MONGO_URL = "localhost"
MONGO_DB = "taobao"
MONGO_COLLECTION = "products"
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
collection = db[MONGO_COLLECTION]
def index_page(page):
"""
抓取索引頁
:param page: 頁碼
"""
print("正在爬取第{}頁".format(page))
try:
url = "https://s.taobao.com/search?q={}".format(keyword)
browser.get(url)
if page > 1:
input = wait.until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,"#mainsrp-pager div.form > input")))
submit = wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,"#mainsrp-pager div.form > span.btn.J_Submit")))
input.clear()
input.send_keys(page)
submit.click()
wait.until(expected_conditions.text_to_be_present_in_element((By.CSS_SELECTOR,"#mainsrp-pager li.item.active > span"),str(page)))
# 商品信息
wait.until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,".m-itemlist .items .item")))
get_products()
except:
index_page(page)
def get_products():
"""
提取商品數據
"""
print("數據提取中....")
html = browser.page_source
doc = pq(html)
items = doc('#mainsrp-itemlist .items .item').items()
for item in items:
image = item.find(".pic-link .img").attr('data-src')
# if image:
# result = re.match('.*?!!(.*?)\..*?', image)
# result = re.sub('[a-z\_\-]', '', result)
# if result:
# id = result.group(1)
# print(id)
data = {
'title': item.find(".title").text().strip(),
'image':image,
'price':item.find(".price").text().strip(),
'deal':item.find('.deal-cnt').text().strip(),
'shop':item.find('.shop').text().strip(),
'location':item.find(".location").text().strip(),
}
print(data)
save_to_mongo(data)
def save_to_mongo(data):
"""
保存數據到mongoDB中
:param data:
:return:
"""
try:
if collection.insert(data):
print("success")
except:
print("fail")
def main():
for i in range(1,101):
index_page(i)
# browser.close()
if __name__ == "__main__":
keyword = input("請輸入關鍵詞:")
main()
```
- 介紹
- 1.開發環境配置
- 1.1 python3的安裝
- 1.1.1 windows下的安裝
- 1.1.2 Linux下的安裝
- 1.1.3 Mac下的安裝
- 1.2 請求庫的安裝
- 1.2.1 requests的安裝
- 1.2.2 selenium的安裝
- 1.2.3 ChromeDriver的安裝
- 1.2.4 GeckoDriver 的安裝
- 1.2.5 PhantomJS的安裝
- 1.2.6 aiohttp的安裝
- 1.3 解析庫的安裝
- 1.3.1 lxml的安裝
- 1.3.2 Beautiful Soup的安裝
- 1.3.3 pyquery的安裝
- 1.3.4 tesserocr的安裝
- 1.4 數據庫的安裝
- 1.4.1 MySQL的安裝
- 1.4.2 MongoDB的安裝
- 1.4.3 Redis的安裝
- 1.5 存儲庫的安裝
- 1.5.1 PyMySQL的安裝
- 1.5.2 PyMongo的安裝
- 1.5.3 redis-py的安裝
- 1.5.4 RedisDump的安裝
- 1.6 Web庫的安裝
- 1.6.1 Flask的安裝
- 1.6.2 Tornado的安裝
- 1.7 App爬取相關庫的安裝
- 1.7.1 Charles的安裝
- 1.7.2 mitmproxy的安裝
- 1.7.3 Appium的安裝
- 1.8 爬蟲框架的安裝
- 1.8.1 pyspider的安裝
- 1.8.2 Scrapy的安裝
- 1.8.3 Scrapy-Splash的安裝
- 1.8.4 ScrapyRedis的安裝
- 1.9 布署相關庫的安裝
- 1.9.1 Docker的安裝
- 1.9.2 Scrapyd的安裝
- 1.9.3 ScrapydClient的安裝
- 1.9.4 ScrapydAPI的安裝
- 1.9.5 Scrapyrt的安裝
- 1.9.6-Gerapy的安裝
- 2.爬蟲基礎
- 2.1 HTTP 基本原理
- 2.1.1 URI和URL
- 2.1.2 超文本
- 2.1.3 HTTP和HTTPS
- 2.1.4 HTTP請求過程
- 2.1.5 請求
- 2.1.6 響應
- 2.2 網頁基礎
- 2.2.1網頁的組成
- 2.2.2 網頁的結構
- 2.2.3 節點樹及節點間的關系
- 2.2.4 選擇器
- 2.3 爬蟲的基本原理
- 2.3.1 爬蟲概述
- 2.3.2 能抓怎樣的數據
- 2.3.3 javascript渲染的頁面
- 2.4 會話和Cookies
- 2.4.1 靜態網頁和動態網頁
- 2.4.2 無狀態HTTP
- 2.4.3 常見誤區
- 2.5 代理的基本原理
- 2.5.1 基本原理
- 2.5.2 代理的作用
- 2.5.3 爬蟲代理
- 2.5.4 代理分類
- 2.5.5 常見代理設置
- 3.基本庫使用
- 3.1 使用urllib
- 3.1.1 發送請求
- 3.1.2 處理異常
- 3.1.3 解析鏈接
- 3.1.4 分析Robots協議
- 3.2 使用requests
- 3.2.1 基本用法
- 3.2.2 高級用法
- 3.3 正則表達式
- 3.4 抓取貓眼電影排行
- 4.解析庫的使用
- 4.1 使用xpath
- 4.2 使用Beautiful Soup
- 4.3 使用pyquery
- 5.數據存儲
- 5.1 文件存儲
- 5.1.1 TXT 文件存儲
- 5.1.2 JSON文件存儲
- 5.1.3 CSV文件存儲
- 5.2 關系型數據庫存儲
- 5.2.1 MySQL的存儲
- 5.3 非關系數據庫存儲
- 5.3.1 MongoDB存儲
- 5.3.2 Redis存儲
- 6.Ajax數據爬取
- 6.1 什么是Ajax
- 6.2 Ajax分析方法
- 6.3 Ajax結果提取
- 6.4 分析Ajax爬取今日頭條街拍美圖
- 7.動態渲染頁面爬取
- 7.1 Selenium的使用
- 7.2 Splash的使用
- 7.3 Splash負載均衡配置
- 7.4 使用selenium爬取淘寶商品
- 8.驗證碼的識別
- 8.1 圖形驗證碼的識別
- 8.2 極驗滑動驗證碼的識別
- 8.3 點觸驗證碼的識別
- 8.4微博宮格驗證碼的識別
- 9.代理的使用
- 9.1 代理的設置
- 9.2 代理池的維護
- 9.3 付費代理的使用
- 9.4 ADSL撥號代理
- 9.5 使用代理爬取微信公總號文章
- 10.模擬登錄
- 10.1 模擬登陸并爬去GitHub
- 10.2 Cookies池的搭建
- 11.App的爬取
- 11.1 Charles的使用
- 11.2 mitmproxy的使用
- 11.3 mitmdump“得到”App電子書信息
- 11.4 Appium的基本使用
- 11.5 Appnium爬取微信朋友圈
- 11.6 Appium+mitmdump爬取京東商品
- 12.pyspider框架的使用
- 12.1 pyspider框架介紹
- 12.2 pyspider的基本使用
- 12.3 pyspider用法詳解
- 13.Scrapy框架的使用
- 13.1 scrapy框架介紹
- 13.2 入門
- 13.3 selector的用法
- 13.4 spider的用法
- 13.5 Downloader Middleware的用法
- 13.6 Spider Middleware的用法
- 13.7 Item Pipeline的用法
- 13.8 Scrapy對接Selenium
- 13.9 Scrapy對接Splash
- 13.10 Scrapy通用爬蟲
- 13.11 Scrapyrt的使用
- 13.12 Scrapy對接Docker
- 13.13 Scrapy爬取新浪微博
- 14.分布式爬蟲
- 14.1 分布式爬蟲原理
- 14.2 Scrapy-Redis源碼解析
- 14.3 Scrapy分布式實現
- 14.4 Bloom Filter的對接
- 15.分布式爬蟲的部署
- 15.1 Scrapyd分布式部署
- 15.2 Scrapyd-Client的使用
- 15.3 Scrapyd對接Docker
- 15.4 Scrapyd批量部署
- 15.5 Gerapy分布式管理
- 微信公總號文章實戰
- 源碼
- other