亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium爬取淘寶實戰(zhàn)練習(xí)
作者:蔣蜀黍 發(fā)布時間:[ 2017/5/5 10:50:36 ] 推薦標簽:功能測試 Selenium

  1、項目流程

  2、中心調(diào)度
# 中心調(diào)度
def main():
try:
total = search()
total = int(re.compile('.*?(d+).*?').search(total).group(1))
for i in range(2,total+1):
next_page(i)
except Exception as e:
print('異常')
finally:
browser.close()
  3、模擬查詢
# 根據(jù)關(guān)鍵字查詢
def search():
try:
browser.get('https://www.taobao.com/')
# 直到搜索框加載出
input_search = wait.until(EC.presence_of_element_located((By.ID,'q')))
# 直到搜索按鈕可以點擊
submit_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'btn-search')))
input_search.send_keys(KEYWORDS)
submit_button.click()
total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'div.wraper  div.total')))
get_products()
return total.text
except TimeoutException as e:
print('響應(yīng)超時')
  4、下一頁的操作
# 下一頁爬取
def next_page(index):
try:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager div.form > input')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager div.form > span.btn.J_Submit')))
input.clear()
input.send_keys(index)
submit.click()
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(index)))
get_products()
except NoSuchElementException as e:
print('元素未加載')
return next_page(index)
  5、商品信息的解析
# 獲取一頁上所有的商品
def get_products():
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-itemlist .items .item")))
html = browser.page_source
doc = PyQuery(html)
items = doc('.m-itemlist .items  .item').items()
for item in items:
product = {
'image':item.find('.pic-link .img').attr('data-src'),
'price':float(item.find('.price').text()[2:]),
'deal':item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text(),
'keywords':KEYWORDS
}
save_to_mongo(product=product)
  6、完整代碼
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymongo
import re
from pyquery import PyQuery
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,NoSuchElementException
from setting import *
client = pymongo.MongoClient(MONGO_HOST)
db = client[MONGO_DB]
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
# 根據(jù)關(guān)鍵字查詢
def search():
try:
browser.get('https://www.taobao.com/')
# 直到搜索框加載出
input_search = wait.until(EC.presence_of_element_located((By.ID,'q')))
# 直到搜索按鈕可以點擊
submit_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'btn-search')))
input_search.send_keys(KEYWORDS)
submit_button.click()
total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'div.wraper  div.total')))
get_products()
return total.text
except TimeoutException as e:
print('響應(yīng)超時')
# 下一頁爬取
def next_page(index):
try:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager div.form > input')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager div.form > span.btn.J_Submit')))
input.clear()
input.send_keys(index)
submit.click()
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(index)))
get_products()
except NoSuchElementException as e:
print('元素未加載')
return next_page(index)
# 獲取一頁上所有的商品
def get_products():
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-itemlist .items .item")))
html = browser.page_source
doc = PyQuery(html)
items = doc('.m-itemlist .items  .item').items()
for item in items:
product = {
'image':item.find('.pic-link .img').attr('data-src'),
'price':float(item.find('.price').text()[2:]),
'deal':item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text(),
'keywords':KEYWORDS
}
save_to_mongo(product=product)
# 保存至mongoDB
def save_to_mongo(product):
try:
if db[MONGO_TABLE].insert(product):
print('保存成功', product)
except Exception:
print('保存失敗')
# 中心調(diào)度
def main():
try:
total = search()
total = int(re.compile('.*?(d+).*?').search(total).group(1))
for i in range(2,total+1):
next_page(i)
except Exception as e:
print('異常')
finally:
browser.close()
if __name__=='__main__':
main()
  7、運行結(jié)果


 

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 日韩欧美在线观看 | a级在线观看| 免费大片黄日本在线观看 | 国产啪精品视频网免费 | 久久精品免视看国产明星 | 日韩激情文学 | 91成人在线 | 一级做a爰片欧美一区 | 国产一级特黄aa大片在线 | 黄色大片视频网站 | 性欧美成人免费观看视 | 亚洲综合网在线观看 | 日本亚洲国产 | 中文字幕一区2区3区 | freefr性欧美69hd | 日日碰狠狠添天天爽对白 | 国产国语高清在线视频二区 | 91短视频在线观看 | 亚洲国产婷婷综合在线精品 | 亚洲伦理在线观看 | 亚洲欧洲中文日产 | 在线观看黄色小视频 | 九九全国免费视频 | 最新欧美精品一区二区三区 | 亚洲第一区视频在线观看 | 日韩操操 | 亚洲视屏一区 | a视频在线观看 | 欧美日韩中文字幕在线观看 | 狠狠躁夜夜躁人人爽天天 | 色视频免费网站 | 一级α一级α片免费观看网站 | 欧美白人最猛性xxxxx | 国内外精品免费视频 | 在线免费污视频 | 日韩国产午夜一区二区三区 | www.激情五月 | 成年人天堂 | 日韩美女拍拍免费视频网站 | 看全色黄一级毛片 | 自拍偷自拍亚洲精品10p |