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

Python使用Selenium模塊實現(xiàn)模擬瀏覽器抓取淘寶商品美食信息功能示例

 更新時間:2018年07月18日 08:47:04   作者:wanlifeipeng  
這篇文章主要介紹了Python使用Selenium模塊實現(xiàn)模擬瀏覽器抓取淘寶商品美食信息功能,涉及Python基于re模塊的正則匹配及selenium模塊的頁面抓取等相關操作技巧,需要的朋友可以參考下

本文實例講述了Python使用Selenium模塊實現(xiàn)模擬瀏覽器抓取淘寶商品美食信息功能。分享給大家供大家參考,具體如下:

import re
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
from bs4 import BeautifulSoup
from pymongo import MongoClient
from pymongo.errors import PyMongoError
url = 'http://www.taobao.com'
KEYWORD = '美食'
# monogdb配置信息
MONGO_HOST = "localhost"
MONGO_DATABASE = "taobao"
MONGO_TABLE = "meishi"
client = MongoClient(host=MONGO_HOST)
db = client[MONGO_DATABASE]
# PhantomJS 命令行相關配置
# 參見 http://phantomjs.org/api/command-line.html
SERVICE_ARGS = ['--disk-cache=true', '--load-images=false']
# driver = webdriver.Chrome() # 有界面
driver = webdriver.PhantomJS(service_args=SERVICE_ARGS) # 無界面
delay = 10
wait = WebDriverWait(driver, delay)
# print('windows size', driver.get_window_size())
# PhantomJs()的瀏覽器窗口很小,寬高只有400 * 300
driver.maximize_window() # 窗口最大化 # 對于PhantomJS來說設置窗口大小很關鍵,如果不設置,經(jīng)常會出現(xiàn)問題
# driver.set_window_size(1920, 1080) # 設置瀏覽器窗口大小
# 模擬在淘寶網(wǎng)頁中輸入關鍵字搜索
def search():
  print("準備搜索 %s" % KEYWORD)
  try:
    driver.get(url)
    input_box = wait.until(
      EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))
    )
    search_btn = wait.until(EC.element_to_be_clickable(
      (By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button')))
    input_box.send_keys(KEYWORD)
    search_btn.click()
    total_page_str = wait.until(
      EC.presence_of_element_located(
        (By.CSS_SELECTOR, 'div.total'))).text
    total_page_num = int(re.search("(\d+)", total_page_str).group(1))
    item_list = get_goods_by_beautifulsoup()
    save_to_mongodb(item_list)
    return total_page_num
  except TimeoutError:
    print("搜索%s超時", KEYWORD)
    print("重新嘗試搜索: %s", KEYWORD)
    search()
# 根據(jù)頁碼獲取指定頁數(shù)據(jù),并將其保存到數(shù)據(jù)庫中
def get_page(page_num):
  print("正在獲取第%d頁數(shù)據(jù)" % page_num)
  try:
    page_num_box = wait.until(
      EC.presence_of_element_located(
        (By.CSS_SELECTOR, "div.form > input")))
    ok_btn = wait.until(EC.element_to_be_clickable(
      (By.CSS_SELECTOR, 'div.form > span.btn.J_Submit')))
    page_num_box.clear()
    page_num_box.send_keys(page_num)
    ok_btn.click()
    wait.until(
      EC.text_to_be_present_in_element(
        (By.CSS_SELECTOR,
         'li.item.active > span.num'),
        str(page_num)))
    item_list = get_goods_by_beautifulsoup()
    save_to_mongodb(item_list)
  except TimeoutException:
    print("請求第%d頁失敗" % page_num)
    print("嘗試重新獲取第%d頁" % page_num)
    return get_page(page_num)
def get_goods_by_pyquery():
  '''
  通過pyquery庫解析數(shù)據(jù)
  獲取商品的圖片url、價格、標題、成交量、店鋪名稱、店鋪位置
  '''
  wait.until(EC.presence_of_element_located(
    (By.CSS_SELECTOR, "#mainsrp-itemlist .items .item")))
  html = driver.page_source
  doc = pq(html)
  items = list(doc('#mainsrp-itemlist .items .item').items())
  for item in items:
    yield {
      # 不要用src屬性,獲取的圖片地址很多是.gif圖片,而非真實商品圖片,
      'image': 'http://' + item.find('.J_ItemPic.img').attr('data-src'),
      'price': item.find('.price').text(),
      'title': item.find('.row > .J_ClickStat').text().strip(),
      'deal_cnt': item.find('.deal-cnt').text()[:-3],
      'shop': item.find('.shop').text(),
      'location': item.find('.location').text(),
    }
# 通過bs4解析數(shù)據(jù)
def get_goods_by_beautifulsoup():
  '''
  通過bs4庫解析數(shù)據(jù)
  獲取商品的圖片url、價格、標題、成交量、店鋪名稱、店鋪位置
  '''
  wait.until(EC.presence_of_element_located(
    (By.CSS_SELECTOR, "#mainsrp-itemlist .items .item")))
  html = driver.page_source
  soup = BeautifulSoup(html, 'lxml')
  items = soup.select('#mainsrp-itemlist .items .item')
  for item in items:
    yield {
      'image': 'http:' + item.select('img.J_ItemPic.img')[0]['data-src'],
      'price': item.select('div.price.g_price.g_price-highlight')[0].get_text(strip=True),
      'title': item.select('div.row.row-2.title > a.J_ClickStat')[0].get_text(strip=True),
      'deal_cnt': item.select('div.deal-cnt')[0].text[:-3],
      'shop': item.select('div.shop > a')[0].get_text(strip=True),
      'location': item.select('div.location')[0].text,
    }
def save_to_mongodb(item_list):
  for item in item_list:
    try:
      db[MONGO_TABLE].insert(item)  # insert支持插入多條數(shù)據(jù)
      print("mongodb插入數(shù)據(jù)成功:", item)
    except PyMongoError as e:
      print("mongodb插入數(shù)據(jù)失敗:", item, e)
# 獲取淘寶美食的圖片url、價格、標題、成交量、店鋪名稱、店鋪位置并將結果保存在mongodb數(shù)據(jù)庫中
if __name__ == '__main__':
  try:
    total_pages = search()
    for page_num in range(2, total_pages + 1):
      get_page(page_num)
  except Exception as e:
    print("出錯了", e)
  finally: # 確保 瀏覽器能正常關閉
    driver.close()

備注:

PhantomJS無界面瀏覽器打開的窗口默認大小400*300, 往往不能將網(wǎng)頁加載完全,會給提取數(shù)據(jù)造成很大的困難,因此需要指定窗口大小。

可以使用 maximize_window() 最大化窗口或者set_window_size()設置指定大小

可能會出現(xiàn)的異常:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Screenshot: available via screen

更多關于Python相關內(nèi)容可查看本站專題:《Python Socket編程技巧總結》、《Python正則表達式用法總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論