一、模拟搜索操作
通过selenium库的css选择器来对搜索框和搜索按钮来进行定位
def search():
try:
browser.get('https://www.taobao.com/')
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#q')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button')))
input.send_keys('美食')
submit.click()
total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total')))
get_products()
return total.text
except TimeoutException:
return search()
然后进行模拟输入搜索词,进行搜索,最终输出搜索页面总数
为了保证页面完全显示,使用wait.until()函数来进行等待条件判断,只有条件加载出来了才能进行下一项
二、实现翻页操作
1.可以模拟点击下一页进行翻页
2.也可以通过模拟输入页码进行翻页
因为得到了页码总数,所以通过输入页码来进行翻页,为了保证翻页后页面加载正确,需要对其进行页码高亮部分进行确认,确认输入页码和高亮页码匹配
def next_page(page_number):
try:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')))
input.clear()
input.send_keys(page_number)
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(page_number)))
get_products()
except TimeoutError:
next_page(page_number)
三、对搜索结果进行获取
通过分析得到的页面代码,可以看到我们需要部分的被包含标签的字符,使用PyQuery库来对所得到的代码进行解析
def get_products():
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:
product = {
'image' : item.find('.pic .img').attr('scr'),
'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(product)
save_to_mongo(product)
四、对得到的结果进行保存
创建一个新的配置文件,在其中添加数据库的url和创建数据库名称及表明
MONGO_URL = 'localhost' MONGO_DB = 'taobao' MONGO_TABLE = 'products'
将得到的产品信息存储到MongoDB中
def save_to_mongo(result):
if db[MONGO_TABLE].insert(result):
print('存储到MongoDB成功')
return True
return False
五、完整代码
import re
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
from config import *
import pymongo
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
def search():
try:
browser.get('https://www.taobao.com/')
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#q')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button')))
input.send_keys('美食')
submit.click()
total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total')))
get_products()
return total.text
except TimeoutException:
return search()
def next_page(page_number):
try:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')))
input.clear()
input.send_keys(page_number)
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(page_number)))
get_products()
except TimeoutError:
next_page(page_number)
def get_products():
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:
product = {
'image' : item.find('.pic .img').attr('scr'),
'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(product)
save_to_mongo(product)
def save_to_mongo(result):
if db[MONGO_TABLE].insert(result):
print('存储到MongoDB成功')
return True
return False
def main():
total = search()
total = int(re.compile('(\d+)').search(total).group(1))
for i in range(2,total+1):
next_page(i)
if __name__ == '__main__':
main()