吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

利用python3来爬取淘宝美食商品列表.

爬取流程:

搜索关键字: 用selenium打开浏览器,模拟输入关键字,并搜索对应的商品列表.
分析页码并翻页,模拟翻页,查看到所有页面的商品列表.
分析并提取商品,利用Pyquery分析源码,解析得到商品列表.
存储到MONGODB数据库,将商品列表信息存储到mongoDB数据库.

环境:

进群:548377875   可以获取不同的PDF哦!

ubuntu16.04

python3.5

python库: selenium ,pyquery, pymongo,re

分析过程如下:

1.找到搜索框: 复制元素

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

 

2.找出搜索按钮元素,并复制

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

 

3.查找美食的总页数

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

 

4.查看 "到第几页的输入框" 的元素.

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

5.确定按钮的元素

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

6.判断翻页的数字是否和当前页码一致,一致则表明该数字是高亮显示.表示当前页的页码.

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

 

7.找到商品列表的元素:

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

mongodb安装:

#wget --no-cookie --no-check-certificate https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.0.2.tgz

#tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.0.2.tgz

#mv mongodb-linux-x86_64-ubuntu1604-4.0.2 mongodb4

#mkdir mongodb4/dbdata

#export PATH=$PATH:mongodb4/bin

启动mongodb:

# nohup mongod --dbpath=/data/services/nosql/mongodb4/dbdata &

具体使用请查看:

菜鸟教程: http://www.runoob.com/mongodb/mongodb-tutorial.html

本篇文章采用:

python3.5调取无界面火狐浏览器访问.并将打印出的数据存入mongoDB数据库.

代码如下:

taobaomeishi.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#导入相关模块:
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
import re
from anli.mongoconfig import *
import pymongo
#设置mongodb客户端访问
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
#设置无界面访问
opt = webdriver.FirefoxOptions()
opt.set_headless()
browser = webdriver.Firefox(options=opt)
#等待浏览器加载页面成功.
wait = WebDriverWait(browser,10)
#打开浏览器,搜索美食,查看所有页数
def search():
try:
# 后台打开浏览器
browser.get('https://www.taobao.com')
# 用CSS选择器复制搜索框
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 .search-button')))
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,'.total')))
# 调取商品列表的函数.
get_products()
return total.text
except TimeoutException: #超时错误.
return search()

 

# 翻页
def next_page(page_number):
try:
#注意在firefox和chrome浏览器复制出来的元素不太一样.
#要传入的页码: 到第几页
input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR,'input.input:nth-child(2)'))
)
#复制确定按钮的元素:
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.btn:nth-child(4)')))
#清除页码
input.clear()
#输入当前页码
input.send_keys(page_number)
#点击确定按钮.
submit.click()
#判断当前页码是否是当前数字: 复制高亮页码数.
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'span.num'),str(page_number)))
get_products()
except TimeoutException:
next_page(page_number)
#解析jquery源码
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 = {
'title': item.find('.title').text(),
'price': item.find('.price').text(),
'image': item.find('.pic .img').attr('src'),
'shop': item.find('.shop').text(),
'deal': item.find('.deal-cnt').text()[:-3],
'location': item.find('.location').text()
}
print(product)
#将商品列表信息保存到mongoDB数据库.
save_to_mongo(product)

 

#保存到mongoDB的函数:
def save_to_mongo(result):
try:
if db[MONGO_TABLE].insert(result):
print('存储到mongodb成功',result)
except Exception:
print('存储到mongodb失败',result)
#执行相关操作:
def main():
total = search()
# 用正则表达式只匹配出数字,并打印数字.
total = int(re.compile('(d+)').search(total).group(1))
print(total)
for i in range(2,total + 1):
next_page(i)
if __name__=='__main__':
main()
#关闭无界面浏览器.
browser.quit()

 

#mongoDB.py配置:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
MONGO_URL = 'localhost'
MONGO_DB = 'taobao'
MONGO_TABLE = 'meishi'

效果如下:

 

吃货们注意了!淘宝美食排行榜!用Python来分析什么最好吃!

 

 

 

网页技术更新比较快.但是爬虫技术万变不离其宗.本人也在学习中. 编程的世界,到处充满着惊喜.不过还需要你脚踏实地的去研究,去探索. 更多的惊喜还等着你去挖掘,还在等什么呢?

chrome驱动和firefox驱动请参考本人博客:

http://blog.51cto.com/liyuanjie/2128660

写的比较详细,本人文笔不是很好,不喜勿喷.谢谢各位支持.

猜你喜欢

转载自www.cnblogs.com/PY08523/p/9662158.html