python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page

Was this helpful?

  1. Crawler

Selenium

Driver -->

  1. PhantomJS速度略快於Chrome,但會在背景執行,無法觀察實際狀況

  2. 載入url建議設定些微時間讓driver等待,讓JS內容完整載入後再擷取原始碼進行處理

  3. 可在driver開啟期間處理原始碼,如:以BeautifulSoup處理第一頁,輸入點擊指令後,再用BS處理第二頁,避免重複啟動driver

p_path = 'driver的exe檔案位置'
web = webdriver.PhantomJS(executable_path=p_path)

#以css種類進行搜尋
web.find_element_by_css_selector('.css1.css2')

#以id進行搜尋
web.find_element_by_id('id123')


#模擬操作則直接加在find結果即可
#ex.清除搜尋框
web.find_element_by_id('search').clear()

#ex.輸入值至搜尋框
web.find_element_by_id('search').send_keys('搜尋內容')

#ex.按下傳送按鈕進行搜索
web.find_element_by_id('submit').click()

#讓driver等一會兒,等待方式看個人需求
time.sleep(1.5)

#ex.獲取搜尋結果網頁原始碼
soup=BeautifulSoup(web.page_source,'lxml')
.
.
.
#用完記得關起來
web.close()
web.quit() #保險
Previousmail codeNextTCP & UDP

Last updated 5 years ago

Was this helpful?

selenium最最最基本實現
三種wait方法的差異
常用方法
觀察JS動態網頁