最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3通過selenium爬蟲獲取到dj商品的實例代碼

 更新時間:2019年04月25日 09:08:05   作者:朱春雨  
這篇文章主要介紹了python3通過selenium爬蟲獲取到dj商品的實例代碼,需要的朋友可以參考下

先給大家介紹下python3 selenium使用

其實這個就相當于模擬人的點擊事件來連續(xù)的訪問瀏覽器。如果你玩過王者榮耀的話在2016年一月份的版本里面就有一個bug。

安卓手機下載一個按鍵精靈就可以在冒險模式里面設(shè)置按鍵,讓手機自動玩闖關(guān),一局19個金幣,一晚上就一個英雄了。不過

程序員也不是吃素的。給一個星期設(shè)置了大概4000金幣上限。有興趣的可以去試試。(注:手機需要root)

進入正題:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

在寫之前需要下載selenium模塊

brguge=webdriver.Chrome()#聲明驅(qū)動對象
try:
  brguge.get('https://www.baidu.com')#發(fā)送get請求
  input=brguge.find_element_by_id('kw')#找到目標

  input.send_keys('python')#輸入python關(guān)鍵字
  input.send_keys(Keys.ENTER)#敲入回車
  wait=WebDriverWait(brguge,10)#等待元素加載出來
  wait.until(EC.presence_of_element_located(By.ID,'content_left'))#加載
  print(brguge.current_url)#輸出搜索的路徑
  print(brguge.get_cookie())#輸出cookie
  print(brguge.page_source)#輸出結(jié)果源代碼
finally:
  brguge.close()#關(guān)閉谷歌瀏覽器

下面是一些selenium模塊的基本用法

查找元素

    單個元素  

(from selenium import webdriver)


    brguge.find_element_by_id('q')用這個元素找id是q的元素
    brguge.find_element_by_css_selector('#q')找css樣式是q的
    brguge.find_element_by_xpath('//*[ @id="q"]')三個效果一樣
    brguge.find_element_by_name()通過name來查找
    brguge.find_element_by_link_text()通過link來查找
    brguge.find_element_by_partial_link_text()
    brguge.find_element_by_tag_name()
    brguge.find_element_by_class_name()通過class查找
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    brguge.find_element(By.ID,'Q')通用查找方式

    多個元素(find_elements)加了個s
        他會以列表的形式打印出來
        brguge.find_elements_by_css_selector('.service-bd li')css樣式為li的元素
        brguge.find_elements(By.css_selector,'.service-bd li')兩個作用一樣
        (利用索引就可以獲取單個或多個元素了)
    元素交互操作(獲取元素然后再給他指令)
        選擇輸入框 --》send_keys('輸入文字')--》clear()清空輸入框--在輸入別的--》找到搜索--》click(點擊)
        input.clear()清空按鈕
    交互動作(將動作附加到動作鏈中串行執(zhí)行)
        switch_to_frame('iframeResult')
        用css樣式分別找到兩個要交互
        調(diào)用ActionChains(調(diào)用谷歌的)
        drag_and_drop(source,target)第一個到第二個上面
        perform()

下面看下python3通過selenium爬蟲獲取到dj商品的實例代碼。

具體代碼如下所示:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from lxml import etree
import time, json
JD_URL_Login = "https://www.jd.com/"
class CustomizeException(Exception):
  def __init__(self, status, msg):
    self.status = status
    self.msg = msg
class JD:
  def __init__(self):
    self.browser = None
    self.__init_browser()
  def __init_browser(self):
    options = Options()
    options.add_argument("--headless")
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    # 設(shè)置為無圖模式
    options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
    self.browser = webdriver.Chrome(options=options)
    # 設(shè)置瀏覽器最大化窗口
    self.browser.maximize_window()
    # 隱式等待時間為3s
    self.browser.implicitly_wait(3)
    self.browser.get(JD_URL_Login)
    self.wait = WebDriverWait(self.browser, 10)
  def __search_goods(self, goods):
    '''搜索商品的方法'''
    self.file = open("jd-{}.json".format(goods), "a", encoding="utf-8")
    self.wait.until(EC.presence_of_all_elements_located((By.ID, "key")))
    serach_input = self.browser.find_element_by_id("key")
    serach_input.clear()
    serach_input.send_keys(goods, Keys.ENTER)
  def __get_goods_info(self, page_source):
    '''從網(wǎng)頁源碼中獲取到想要的數(shù)據(jù)'''
    selector_html = etree.HTML(page_source)
    # 商品名字 不要獲取title屬性,以后再改吧,最好是獲取到商品名的文本內(nèi)容
    goods_name = selector_html.xpath("http://div[@class='gl-i-wrap']//div[contains(@class,'p-name')]/a/@title")
    # 商品價格
    goods_price = selector_html.xpath("http://div[@class='gl-i-wrap']//div[@class='p-price']/strong/i/text()")
    # 商品評價數(shù)量
    comment_num_selector = selector_html.xpath("http://div[@class='p-commit']/strong")
    comment_num = [selector.xpath("string(.)") for selector in comment_num_selector]
    # 商品店鋪
    shop_name = selector_html.xpath("http://a[@class='curr-shop']/text()")
    goods_zip = zip(goods_name, goods_price, comment_num, shop_name)
    for goods_info in goods_zip:
      dic = {}
      dic["goods_name"] = goods_info[0]
      dic["goods_price"] = goods_info[1]
      dic["comment_num"] = goods_info[2]
      dic["shop_name"] = goods_info[3]
      # print("商品名字>>:", goods_info[0])
      # print("商品價格>>:", goods_info[1])
      # print("商品評價數(shù)量>>:", goods_info[2])
      # print("商品店鋪>>:", goods_info[3])
      # print("*" * 100)
      yield dic
  def __swipe_page(self):
    '''上下滑動頁面,將完整的網(wǎng)頁源碼返回'''
    height = self.browser.execute_script("return document.body.scrollHeight;")
    js = "window.scrollTo(0, {});".format(height)
    self.browser.execute_script(js)
    while True:
      time.sleep(1)
      now_height = self.browser.execute_script("return document.body.scrollHeight;")
      if height == now_height:
        return self.browser.page_source
      js = "window.scrollTo({}, {});".format(height, now_height)
      self.browser.execute_script(js)
      height = now_height
  def __is_element_exists(self, xpath):
    '''檢測一個xpath是否能夠找到'''
    try:
      self.browser.find_element_by_xpath(xpath=xpath)
      return True
    except NoSuchElementException:
      return False
  def __click_next_page(self):
    '''點擊下一頁,實現(xiàn)翻頁功能'''
    self.wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "pn-next")))
    xpath = "http://a[@class='pn-next']"
    if not self.__is_element_exists(xpath):
      raise CustomizeException(10000, "該商品訪問完畢")
    self.browser.find_element_by_xpath(xpath).click()
  def __write_to_json(self, dic: dict):
    data_json = json.dumps(dic, ensure_ascii=False)
    self.file.write(data_json + "\n")
  def run(self, goods):
    self.__search_goods(goods)
    n = 1
    while True:
      print("正在爬取商品 <{}>---第{}頁......".format(goods, n))
      time.sleep(3)
      html = self.__swipe_page()
      for dic in self.__get_goods_info(html):
        self.__write_to_json(dic)
      try:
        self.__click_next_page()
      except CustomizeException:
        try:
          goods = goods_list.pop(0)
          self.run(goods)
        except IndexError:
          return
      n += 1
  def __del__(self):
    self.browser.close()
    self.file.close()
if __name__ == '__main__':
  jd = JD()
  goods_list = ["純牛奶", "酸奶", "奶茶", "床上用品", "電磁爐", "電視", "小米筆記本", "華碩筆記本", "聯(lián)想筆記本", "男士洗面奶", "女士洗面奶", "沐浴露", "洗發(fā)露",
         "牙刷", "牙膏", "拖鞋", "剃須刀", "水手服", "運動服", "紅龍果", "蘋果", "香蕉", "洗衣液", "電飯煲"]
  try:
    goods = goods_list.pop(0)
  except IndexError:
    raise CustomizeException(20000, "goods_list不能為空")
  try:
    jd.run(goods)
  finally:
    del jd

總結(jié)

以上所述是小編給大家介紹的python3通過selenium爬蟲獲取到dj商品的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • windows下python連接oracle數(shù)據(jù)庫

    windows下python連接oracle數(shù)據(jù)庫

    這篇文章主要為大家詳細介紹了windows下python連接oracle數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 本地部署Python?Flask并搭建web問答應(yīng)用程序框架實現(xiàn)遠程訪問的操作方法

    本地部署Python?Flask并搭建web問答應(yīng)用程序框架實現(xiàn)遠程訪問的操作方法

    Flask是一個Python編寫的Web微框架,使用Python語言快速實現(xiàn)一個網(wǎng)站或Web服務(wù),本期教程我們使用Python Flask搭建一個web問答應(yīng)用程序框架,并結(jié)合cpolar內(nèi)網(wǎng)穿透工具將我們的應(yīng)用程序發(fā)布到公共網(wǎng)絡(luò)上,實現(xiàn)可多人遠程進入到該web應(yīng)用程序訪問,需要的朋友可以參考下
    2023-12-12
  • python閉包、深淺拷貝、垃圾回收、with語句知識點匯總

    python閉包、深淺拷貝、垃圾回收、with語句知識點匯總

    在本篇文章里小編給大家整理了關(guān)于python閉包、深淺拷貝、垃圾回收、with語句知識點匯總,有興趣的朋友們學(xué)習(xí)下。
    2020-03-03
  • 基于Pygame實現(xiàn)簡單的貪吃蛇游戲

    基于Pygame實現(xiàn)簡單的貪吃蛇游戲

    Pygame是一個專門用來開發(fā)游戲的Python模塊,主要用于開發(fā)、設(shè)計?2D?電子游戲。本文主要為大家介紹了通過Pygame制作一個簡單的貪吃蛇游戲,感興趣的同學(xué)可以關(guān)注一下
    2021-12-12
  • 用python實現(xiàn)名片管理系統(tǒng)

    用python實現(xiàn)名片管理系統(tǒng)

    這篇文章主要為大家詳細介紹了用python實現(xiàn)名片管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Python實現(xiàn)隨機創(chuàng)建電話號碼的方法示例

    Python實現(xiàn)隨機創(chuàng)建電話號碼的方法示例

    這篇文章主要介紹了Python實現(xiàn)隨機創(chuàng)建電話號碼的方法,涉及Python隨機數(shù)運算相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • Python實現(xiàn)選擇排序

    Python實現(xiàn)選擇排序

    選擇法也算是入門的一種排序算法,比起冒泡法,它的方法巧妙了一些,它的出發(fā)點在于“挑”,每次挑選數(shù)組的最值,與前置元素換位,然后繼續(xù)挑選剩余元素的最值并重復(fù)操作。個人認為選擇排序的意義不在于排序本身,而在于挑選和置換的方法,對于一些問題很有幫助。
    2017-06-06
  • 如何用Python數(shù)據(jù)可視化來分析用戶留存率

    如何用Python數(shù)據(jù)可視化來分析用戶留存率

    今天和大家來分享一些數(shù)據(jù)可視化方向的干貨,我們來嘗試用Python來繪制一下“漏斗圖”,感興趣的小伙伴和小編一起進入課題吧,但愿大家會有所收獲
    2021-09-09
  • python實現(xiàn)翻轉(zhuǎn)棋游戲(othello)

    python實現(xiàn)翻轉(zhuǎn)棋游戲(othello)

    這篇文章主要為大家詳細介紹了python實現(xiàn)翻轉(zhuǎn)棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Pygame游戲開發(fā)之太空射擊實戰(zhàn)精靈的使用上篇

    Pygame游戲開發(fā)之太空射擊實戰(zhàn)精靈的使用上篇

    相信大多數(shù)8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來自己動手實現(xiàn)它,在編寫學(xué)習(xí)中回顧過往展望未來,下面開始講解精靈的使用
    2022-08-08

最新評論

曲周县| 桃园县| 长寿区| 平江县| 二手房| 清水河县| 社会| 隆回县| 随州市| 新泰市| 柳江县| 安徽省| 榆社县| 云南省| 阳泉市| 靖边县| 清新县| 呼伦贝尔市| 宁夏| 游戏| 竹溪县| 贵州省| 丰宁| 成武县| 日土县| 兴隆县| 昆山市| 平舆县| 南江县| 新郑市| 于田县| 芮城县| 阳曲县| 汝南县| 高尔夫| 阳朔县| 海门市| 铁岭市| 犍为县| 延庆县| 广安市|