python3實(shí)現(xiàn)爬取淘寶美食代碼分享
環(huán)境:
ubuntu16.04
python3.5
python庫(kù): selenium, pyquery,pymongo, re
要求:
設(shè)置×××面瀏覽器訪問(wèn),并將商品列表存入mongoDB數(shù)據(jù)庫(kù).
分析過(guò)程暫時(shí)略過(guò)
代碼:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
1.爬取淘寶美食的流程
- 搜索關(guān)鍵字: 用selenium打開(kāi)瀏覽器,模擬輸入關(guān)鍵字,并搜索對(duì)應(yīng)的商品列表.
- 分析頁(yè)碼并翻頁(yè),模擬翻頁(yè),查看到所有頁(yè)面的商品列表.
- 分析并提取商品,利用Pyquery分析源碼,解析得到商品列表.
- 存儲(chǔ)到MONGODB數(shù)據(jù)庫(kù),將商品列表信息存儲(chǔ)到mongoDB數(shù)據(jù)庫(kù).
"""
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
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
#設(shè)置×××面訪問(wèn)
opt = webdriver.FirefoxOptions()
opt.set_headless()
browser = webdriver.Firefox(options=opt)
#等待瀏覽器加載頁(yè)面成功.
wait = WebDriverWait(browser,10)
def search():
try:
# 后臺(tái)打開(kāi)瀏覽器
browser.get('https://www.taobao.com')
# 用CSS選擇器復(fù)制搜索框
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')))
# 輸入關(guān)鍵字
input.send_keys('美食')
# 點(diǎn)擊搜索按鈕.
submit.click()
# 輸出總共的頁(yè)數(shù).
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.total')))
# 調(diào)取商品列表的函數(shù).
get_products()
return total.text
except TimeoutException: #超時(shí)錯(cuò)誤.
return search()
# 翻頁(yè)
def next_page(page_number):
try:
#注意在firefox和chrome瀏覽器復(fù)制出來(lái)的元素不太一樣.
#要傳入的頁(yè)碼: 到第幾頁(yè)
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR,'input.input:nth-child(2)'))
)
#復(fù)制確定按鈕的元素:
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.btn:nth-child(4)')))
#清除頁(yè)碼
input.clear()
#輸入當(dāng)前頁(yè)碼
input.send_keys(page_number)
#點(diǎn)擊確定按鈕.
submit.click()
#判斷當(dāng)前頁(yè)碼是否是當(dāng)前數(shù)字: 復(fù)制高亮頁(yè)碼數(shù).
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')))
# 獲取網(wǎng)頁(yè)源碼
html = browser.page_source
doc = pq(html)
items = doc('#mainsrp-itemlist .items .item').items()
for item in items:
#定義商品列表詳細(xì)信息的字典
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數(shù)據(jù)庫(kù).
save_to_mongo(product)
def save_to_mongo(result):
try:
if db[MONGO_TABLE].insert(result):
print('存儲(chǔ)到mongodb成功',result)
except Exception:
print('存儲(chǔ)到mongodb失敗',result)
def main():
total = search()
# 用正則表達(dá)式只匹配出數(shù)字,并打印數(shù)字.
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()
#關(guān)閉×××面瀏覽器.
browser.quit()
#mongoconfig.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
MONGO_URL = 'localhost'
MONGO_DB = 'taobao'
MONGO_TABLE = 'meishi'
安裝和使用mongodb參考:
//www.dhdzp.com/article/54758.htm
實(shí)現(xiàn)效果:

注意:
chrome瀏覽器和firefox瀏覽器的網(wǎng)頁(yè)元素不太一樣.
- python 爬蟲(chóng)一鍵爬取 淘寶天貓寶貝頁(yè)面主圖顏色圖和詳情圖的教程
- python定向爬取淘寶商品價(jià)格
- python爬蟲(chóng)爬取淘寶商品信息(selenum+phontomjs)
- Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè)
- python爬蟲(chóng)爬取淘寶商品信息
- python爬取淘寶商品詳情頁(yè)數(shù)據(jù)
- python3爬取淘寶信息代碼分析
- python登錄并爬取淘寶信息代碼示例
- 通過(guò)抓取淘寶評(píng)論為例講解Python爬取ajax動(dòng)態(tài)生成的數(shù)據(jù)(經(jīng)典)
- python實(shí)現(xiàn)爬取千萬(wàn)淘寶商品的方法
- python爬取淘寶商品銷量信息
相關(guān)文章
Python實(shí)現(xiàn)地圖可視化folium完整過(guò)程
Folium是一個(gè)基于leaflet.js的Python地圖庫(kù),其中,Leaflet是一個(gè)非常輕的前端地圖可視化庫(kù),本文重點(diǎn)給大家介紹Python實(shí)現(xiàn)地圖可視化folium完整過(guò)程,感興趣的朋友跟隨小編一起看看吧2021-05-05
python讀取當(dāng)前目錄下的CSV文件數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python讀取當(dāng)前目錄下的CSV文件數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
pymysql.err.DataError:1366的報(bào)錯(cuò)解決
通過(guò)python把數(shù)據(jù)同步至mysql數(shù)據(jù)庫(kù)的過(guò)程中,遇到錯(cuò)誤,本文主要介紹了pymysql.err.DataError:1366的報(bào)錯(cuò)解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Python中字典的基礎(chǔ)知識(shí)歸納小結(jié)
這篇文章主要介紹了Python中字典的基礎(chǔ)知識(shí)歸納小結(jié),都是Python入門(mén)學(xué)習(xí)中的基本知識(shí),值得反復(fù)鞏固:)需要的朋友可以參考下2015-08-08
使用Python實(shí)現(xiàn)批量分割PDF文件
這篇文章主要為大家詳細(xì)介紹了如何使用Python進(jìn)行批量分割PDF文件功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
Python+Selenium定位不到元素常見(jiàn)原因及解決辦法(報(bào):NoSuchElementException)
這篇文章主要介紹了Python+Selenium定位不到元素常見(jiàn)原因及解決辦法(報(bào):NoSuchElementException),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
詳解Python的Flask框架中生成SECRET_KEY密鑰的方法
密鑰值的生成功能十分重要,幾乎也是各大Web開(kāi)發(fā)框架的標(biāo)配,Flask當(dāng)然也不例外,這里我們就來(lái)詳解Python的Flask框架中生成SECRET_KEY密鑰的方法2016-06-06

