欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python進(jìn)階之使用selenium爬取淘寶商品信息功能示例

 更新時(shí)間:2019年09月16日 09:40:14   作者:Jonny工作室  
這篇文章主要介紹了Python進(jìn)階之使用selenium爬取淘寶商品信息功能,結(jié)合實(shí)例形式詳細(xì)分析了Python使用selenium與requests模塊爬取淘寶商品信息的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python進(jìn)階之使用selenium爬取淘寶商品信息功能。分享給大家供大家參考,具體如下:

# encoding=utf-8
__author__ = 'Jonny'
__location__ = '西安'
__date__ = '2018-05-14'
'''
需要的基本開(kāi)發(fā)庫(kù)文件:
requests,pymongo,pyquery,selenium
開(kāi)發(fā)流程:
  搜索關(guān)鍵字:利用selenium驅(qū)動(dòng)瀏覽器搜索關(guān)鍵字,得到查詢后的商品列表
  分析頁(yè)碼并翻頁(yè):得到商品頁(yè)碼數(shù),模擬翻頁(yè),得到后續(xù)頁(yè)面的商品列表
  分析提取商品內(nèi)容:利用PyQuery分析頁(yè)面源代碼,解析獲得商品列表信息
  存儲(chǔ)到MongDB中:將商品的信息列表存儲(chǔ)到數(shù)據(jù)庫(kù)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:
    #判斷頁(yè)面加載是夠成功,設(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()
    #等待頁(yè)面加載完成,便于準(zhǔn)確判斷網(wǎng)頁(yè)的總頁(yè)數(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)翻頁(yè)
def next_page(page_number):
  try:
    # 判斷頁(yè)面加載是夠成功,設(shè)置等待時(shí)間
    # 判斷位置1:頁(yè)面跳轉(zhuǎn)輸入頁(yè)是否加載完成
    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()
    #判斷翻頁(yè)是否成功
    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ì)頁(yè)面進(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ù)庫(kù)
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)
  #獲取總頁(yè)數(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)文章

  • 用Python?Tkinter庫(kù)GUI編程創(chuàng)建圖形用戶界面

    用Python?Tkinter庫(kù)GUI編程創(chuàng)建圖形用戶界面

    這篇文章主要為大家介紹了用Python?Tkinter庫(kù)GUI編程創(chuàng)建圖形用戶界面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Python函數(shù)參數(shù)類型*、**的區(qū)別

    Python函數(shù)參數(shù)類型*、**的區(qū)別

    這篇文章主要介紹了Python函數(shù)參數(shù)類型*、**的區(qū)別,本文用實(shí)例講解它們的區(qū)別,并講解了閉包的相關(guān)知識(shí),需要的朋友可以參考下
    2015-04-04
  • 使用Py2Exe for Python3創(chuàng)建自己的exe程序示例

    使用Py2Exe for Python3創(chuàng)建自己的exe程序示例

    今天小編就為大家分享一篇使用Py2Exe for Python3創(chuàng)建自己的exe程序示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python?文件與文件對(duì)象及文件打開(kāi)關(guān)閉

    Python?文件與文件對(duì)象及文件打開(kāi)關(guān)閉

    這篇文章主要介紹了Python?中的文件與文件對(duì)象,Python中常有的數(shù)據(jù)文件類型有文本文件、二進(jìn)制文件和CSV文件,文本文件是ASCII編碼,漢子存儲(chǔ)的是機(jī)內(nèi)碼,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • python openpyxl方法 zip函數(shù)用法及說(shuō)明

    python openpyxl方法 zip函數(shù)用法及說(shuō)明

    這篇文章主要介紹了python openpyxl方法 zip函數(shù)用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整)

    Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整)

    這篇文章主要介紹了Pytorch四維Tensor轉(zhuǎn)圖片并保存方式(維度順序調(diào)整),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python之Socket網(wǎng)絡(luò)編程詳解

    python之Socket網(wǎng)絡(luò)編程詳解

    這篇文章主要為大家詳細(xì)介紹了python之Socket網(wǎng)絡(luò)編程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • python傳參時(shí)一個(gè)星號(hào)和兩個(gè)星號(hào)的區(qū)別小結(jié)

    python傳參時(shí)一個(gè)星號(hào)和兩個(gè)星號(hào)的區(qū)別小結(jié)

    在Python中,一個(gè)星號(hào)(*)和兩個(gè)星號(hào)(**)用于函數(shù)定義中的參數(shù)傳遞,本文主要介紹了python傳參時(shí)一個(gè)星號(hào)和兩個(gè)星號(hào)的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Python中基礎(chǔ)的socket編程實(shí)戰(zhàn)攻略

    Python中基礎(chǔ)的socket編程實(shí)戰(zhàn)攻略

    Python擁有內(nèi)置的socket模塊,可以用簡(jiǎn)潔明了的代碼來(lái)進(jìn)行socket通信操作,這里我們就為大家整理了一份Python中基礎(chǔ)的socket編程實(shí)戰(zhàn)攻略,需要的朋友可以參考下.
    2016-06-06
  • Python獲取svn版本信息

    Python獲取svn版本信息

    本文主要介紹了Python獲取svn版本信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論