日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 1773 0

本文實(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)鍵字,得到查詢(xú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)容可查看本站專(zhuān)題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 子洲县| 搜索| 九江市| 彭阳县| 若尔盖县| 清涧县| 若羌县| 嘉荫县| 枣强县| 莱西市| 江阴市| 安多县| 平定县| 秦安县| 临猗县| 镇康县| 拜城县| 潞城市| 三穗县| 宜昌市| 同德县| 金山区| 大宁县| 旬邑县| 商洛市| 平湖市| 桃源县| 长白| 观塘区| 旅游| 阳曲县| 怀来县| 句容市| 延边| 泽库县| 武宁县| 卢龙县| 砚山县| 即墨市| 和静县| 静乐县|