欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python中如何使用selenium爬取淘寶商品信息-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Python中如何使用selenium爬取淘寶商品信息的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供同安企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為同安眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

具體如下:

# encoding=utf-8
__author__ = 'Jonny'
__location__ = '西安'
__date__ = '2018-05-14'
'''
需要的基本開發(fā)庫(kù)文件:
requests,pymongo,pyquery,selenium
開發(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中如何使用selenium爬取淘寶商品信息”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

網(wǎng)頁(yè)題目:Python中如何使用selenium爬取淘寶商品信息-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://chinadenli.net/article40/doigho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、做網(wǎng)站微信公眾號(hào)、品牌網(wǎng)站建設(shè)、服務(wù)器托管標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)