Python進階之使用selenium爬取淘寶商品信息功能示例
本文實例講述了Python進階之使用selenium爬取淘寶商品信息功能。分享給大家供大家參考,具體如下:
# encoding=utf-8
__author__ = 'Jonny'
__location__ = '西安'
__date__ = '2018-05-14'
'''
需要的基本開發(fā)庫文件:
requests,pymongo,pyquery,selenium
開發(fā)流程:
搜索關(guān)鍵字:利用selenium驅(qū)動瀏覽器搜索關(guān)鍵字,得到查詢后的商品列表
分析頁碼并翻頁:得到商品頁碼數(shù),模擬翻頁,得到后續(xù)頁面的商品列表
分析提取商品內(nèi)容:利用PyQuery分析頁面源代碼,解析獲得商品列表信息
存儲到MongDB中:將商品的信息列表存儲到數(shù)據(jù)庫MongoDB。
'''
import requests
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 pymongo
import re
import time
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
client = pymongo.MongoClient('localhost',27017)
mongo = client['taobao']
def searcher():
url = 'https://www.taobao.com/'
browser.get(url=url)
try:
#判斷頁面加載是夠成功,設(shè)置等待時間
#判斷位置1:搜索輸入框是否加載完成
input_kw = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))
)
#判斷位置2:搜索輸入框?qū)?yīng)的搜索按鍵是否加載完成
submit = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button'))
)
input_kw.send_keys('男裝')
submit.click()
#等待頁面加載完成,便于準(zhǔn)確判斷網(wǎng)頁的總頁數(shù)
page_counts = wait.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total'))
)
parse_page()
return page_counts.text
except TimeoutException as e:
print(e.args)
return searcher()
#實現(xiàn)翻頁
def next_page(page_number):
try:
# 判斷頁面加載是夠成功,設(shè)置等待時間
# 判斷位置1:頁面跳轉(zhuǎn)輸入頁是否加載完成
input_page = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input'))
)
# 判斷位置2:確認按鍵是否加載完成
submit = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit'))
)
input_page.send_keys(page_number)
submit.click()
#判斷翻頁是否成功
wait.until(EC.text_to_be_present_in_element(
(By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active'),str(page_number))
)
parse_page()
except TimeoutException as e:
print(e.args)
next_page(page_number)
#對頁面進行數(shù)據(jù)處理
def parse_page():
# wait.until(EC.presence_of_element_located(By.CSS_SELECTOR,'#mainsrp-itemlist > div > div'))
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:
goods = {
'image':item.find('.pic .img').attr('src'),
'price':item.find('.price').text(),
'deal':item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text()
}
print(goods)
data_storage(goods)
#將數(shù)據(jù)存入數(shù)據(jù)庫
def data_storage(goods):
try:
if mongo['mongo_sheet'].insert(goods):
print('Successfully storage!')
except Exception as e:
print('failedly storage!',goods)
def main():
text = searcher()
print(text)
#獲取總頁數(shù)
pages = int(re.compile('(\d+)').search(text).group(0))
print(pages)
for i in range(2,pages+1):
next_page(i)
browser.close()
if __name__ == '__main__':
main()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
用Python?Tkinter庫GUI編程創(chuàng)建圖形用戶界面
這篇文章主要為大家介紹了用Python?Tkinter庫GUI編程創(chuàng)建圖形用戶界面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
Python函數(shù)參數(shù)類型*、**的區(qū)別
這篇文章主要介紹了Python函數(shù)參數(shù)類型*、**的區(qū)別,本文用實例講解它們的區(qū)別,并講解了閉包的相關(guān)知識,需要的朋友可以參考下2015-04-04
使用Py2Exe for Python3創(chuàng)建自己的exe程序示例
今天小編就為大家分享一篇使用Py2Exe for Python3創(chuàng)建自己的exe程序示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python openpyxl方法 zip函數(shù)用法及說明
這篇文章主要介紹了python openpyxl方法 zip函數(shù)用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整)
這篇文章主要介紹了Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
python之Socket網(wǎng)絡(luò)編程詳解
這篇文章主要為大家詳細介紹了python之Socket網(wǎng)絡(luò)編程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
python傳參時一個星號和兩個星號的區(qū)別小結(jié)
在Python中,一個星號(*)和兩個星號(**)用于函數(shù)定義中的參數(shù)傳遞,本文主要介紹了python傳參時一個星號和兩個星號的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-02-02
Python中基礎(chǔ)的socket編程實戰(zhàn)攻略
Python擁有內(nèi)置的socket模塊,可以用簡潔明了的代碼來進行socket通信操作,這里我們就為大家整理了一份Python中基礎(chǔ)的socket編程實戰(zhàn)攻略,需要的朋友可以參考下.2016-06-06

