1. 程式人生 > >吃貨們註意了!淘寶美食排行榜!用Python來分析什麽最好吃!

吃貨們註意了!淘寶美食排行榜!用Python來分析什麽最好吃!

不同的 文章 end cap mongodb安裝 exc 比較 mongodb log

利用python3來爬取淘寶美食商品列表.

爬取流程:

搜索關鍵字: 用selenium打開瀏覽器,模擬輸入關鍵字,並搜索對應的商品列表.
分析頁碼並翻頁,模擬翻頁,查看到所有頁面的商品列表.
分析並提取商品,利用Pyquery分析源碼,解析得到商品列表.
存儲到MONGODB數據庫,將商品列表信息存儲到mongoDB數據庫.

環境:

進群:548377875 可以獲取不同的PDF哦!

ubuntu16.04

python3.5

python庫: selenium ,pyquery, pymongo,re

分析過程如下:

1.找到搜索框: 復制元素

技術分享圖片

2.找出搜索按鈕元素,並復制

技術分享圖片

3.查找美食的總頁數

技術分享圖片

4.查看 "到第幾頁的輸入框" 的元素.

技術分享圖片

5.確定按鈕的元素

技術分享圖片

6.判斷翻頁的數字是否和當前頁碼一致,一致則表明該數字是高亮顯示.表示當前頁的頁碼.

技術分享圖片

7.找到商品列表的元素:

技術分享圖片

技術分享圖片

技術分享圖片

mongodb安裝:

#wget --no-cookie --no-check-certificate https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.0.2.tgz

#tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.0.2.tgz

#mv mongodb-linux-x86_64-ubuntu1604-4.0.2 mongodb4

#mkdir mongodb4/dbdata

#export PATH=$PATH:mongodb4/bin

啟動mongodb:

# nohup mongod --dbpath=/data/services/nosql/mongodb4/dbdata &

具體使用請查看:

菜鳥教程: http://www.runoob.com/mongodb/mongodb-tutorial.html

本篇文章采用:

python3.5調取無界面火狐瀏覽器訪問.並將打印出的數據存入mongoDB數據庫.

代碼如下:

taobaomeishi.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#導入相關模塊:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from pyquery import PyQuery as pq
import re
from anli.mongoconfig import *
import pymongo
#設置mongodb客戶端訪問
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
#設置無界面訪問
opt = webdriver.FirefoxOptions()
opt.set_headless()
browser = webdriver.Firefox(options=opt)
#等待瀏覽器加載頁面成功.
wait = WebDriverWait(browser,10)
#打開瀏覽器,搜索美食,查看所有頁數
def search():
try:
# 後臺打開瀏覽器
browser.get(‘https://www.taobao.com‘)
# 用CSS選擇器復制搜索框
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ‘#q‘))
)
# 找到搜索按鈕.
submit = wait.until(
# EC.element_to_be_clickable((By.CSS_SELECTOR,‘#J_TSearchForm .search-button‘)))
EC.element_to_be_clickable((By.CSS_SELECTOR,‘#J_TSearchForm > div.search-button > button‘)))
# 輸入關鍵字
input.send_keys(‘美食‘)
# 點擊搜索按鈕.
submit.click()

# 輸出總共的頁數.
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,‘.total‘)))
# 調取商品列表的函數.
get_products()
return total.text
except TimeoutException: #超時錯誤.
return search()

# 翻頁
def next_page(page_number):
try:
#註意在firefox和chrome瀏覽器復制出來的元素不太一樣.
#要傳入的頁碼: 到第幾頁
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR,‘input.input:nth-child(2)‘))
)
#復制確定按鈕的元素:
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,‘span.btn:nth-child(4)‘)))
#清除頁碼
input.clear()
#輸入當前頁碼
input.send_keys(page_number)
#點擊確定按鈕.
submit.click()
#判斷當前頁碼是否是當前數字: 復制高亮頁碼數.
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,‘span.num‘),str(page_number)))
get_products()
except TimeoutException:
next_page(page_number)
#解析jquery源碼
def get_products():
#商品列表:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,‘#mainsrp-itemlist .items .item‘)))
# 獲取網頁源碼
html = browser.page_source
doc = pq(html)
items = doc(‘#mainsrp-itemlist .items .item‘).items()
for item in items:
#定義商品列表詳細信息的字典
product = {
‘title‘: item.find(‘.title‘).text(),
‘price‘: item.find(‘.price‘).text(),
‘image‘: item.find(‘.pic .img‘).attr(‘src‘),
‘shop‘: item.find(‘.shop‘).text(),
‘deal‘: item.find(‘.deal-cnt‘).text()[:-3],
‘location‘: item.find(‘.location‘).text()
}
print(product)
#將商品列表信息保存到mongoDB數據庫.
save_to_mongo(product)

#保存到mongoDB的函數:
def save_to_mongo(result):
try:
if db[MONGO_TABLE].insert(result):
print(‘存儲到mongodb成功‘,result)
except Exception:
print(‘存儲到mongodb失敗‘,result)
#執行相關操作:
def main():
total = search()
# 用正則表達式只匹配出數字,並打印數字.
total = int(re.compile(‘(d+)‘).search(total).group(1))
print(total)
for i in range(2,total + 1):
next_page(i)
if __name__==‘__main__‘:
main()
#關閉無界面瀏覽器.
browser.quit()

#mongoDB.py配置:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
MONGO_URL = ‘localhost‘
MONGO_DB = ‘taobao‘
MONGO_TABLE = ‘meishi‘

效果如下:

技術分享圖片

網頁技術更新比較快.但是爬蟲技術萬變不離其宗.本人也在學習中. 編程的世界,到處充滿著驚喜.不過還需要你腳踏實地的去研究,去探索. 更多的驚喜還等著你去挖掘,還在等什麽呢?

chrome驅動和firefox驅動請參考本人博客:

http://blog.51cto.com/liyuanjie/2128660

寫的比較詳細,本人文筆不是很好,不喜勿噴.謝謝各位支持.

吃貨們註意了!淘寶美食排行榜!用Python來分析什麽最好吃!