Python進(jìn)階之使用selenium爬取淘寶商品信息功能示例
本文實(shí)例講述了Python進(jìn)階之使用selenium爬取淘寶商品信息功能。分享給大家供大家參考,具體如下:
# encoding=utf-8
__author__ = 'Jonny'
__location__ = '西安'
__date__ = '2018-05-14'
'''
需要的基本開發(fā)庫文件:
requests,pymongo,pyquery,selenium
開發(fā)流程:
搜索關(guān)鍵字:利用selenium驅(qū)動(dòng)瀏覽器搜索關(guān)鍵字,得到查詢后的商品列表
分析頁碼并翻頁:得到商品頁碼數(shù),模擬翻頁,得到后續(xù)頁面的商品列表
分析提取商品內(nèi)容:利用PyQuery分析頁面源代碼,解析獲得商品列表信息
存儲(chǔ)到MongDB中:將商品的信息列表存儲(chǔ)到數(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è)置等待時(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()
#實(shí)現(xiàn)翻頁
def next_page(page_number):
try:
# 判斷頁面加載是夠成功,設(shè)置等待時(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:確認(rèn)按鍵是否加載完成
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)
#對(duì)頁面進(jìn)行數(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正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
PyCharm中關(guān)于安裝第三方包的三個(gè)建議
這篇文章主要介紹了PyCharm中關(guān)于安裝第三方包的三個(gè)建議,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
僅用500行Python代碼實(shí)現(xiàn)一個(gè)英文解析器的教程
這篇文章主要介紹了僅用500行Python代碼實(shí)現(xiàn)一個(gè)英文解析器的教程,自然語言處理近來也是業(yè)界中一個(gè)熱門課題,作者為NLP方向的開發(fā)者,需要的朋友可以參考下2015-04-04
Tensorflow tf.tile()的用法實(shí)例分析
這篇文章主要介紹了Tensorflow tf.tile()的用法實(shí)例分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn)
這篇文章主要介紹了淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python pandas RFM模型應(yīng)用實(shí)例詳解
這篇文章主要介紹了Python pandas RFM模型應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了pandas RFM模型的概念、原理、應(yīng)用及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
win8.1安裝Python 2.7版環(huán)境圖文詳解
在本篇內(nèi)容里小編給大家分享了關(guān)于win8.1安裝Python 2.7版環(huán)境的詳細(xì)步驟和方法,有興趣的朋友們跟著學(xué)習(xí)下。2019-07-07
python使用requests實(shí)現(xiàn)發(fā)送帶文件請(qǐng)求功能
這篇文章主要介紹了python使用requests實(shí)現(xiàn)發(fā)送帶文件請(qǐng)求,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

