爬虫实例——爬取淘女郎相册(通过selenium、PhantomJS、BeautifulSoup爬取)
环境
操作系统:CentOS 6.7 32-bit
Python版本:2.6.6
第三方插件
selenium
PhantomJS
BeautifulSoup
代码
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
'''
作者:昨夜星辰
'''
import re
import os
import time
import shutil
import requests
import subprocess
from bs4 import BeautifulSoup
from selenium import webdriver # 拼接url
def joint_url(string):
return 'https:' + string # 判断文件夹是否存在,如果存在就删除,否则就创建。
def create_folder(path):
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
os.mkdir(path) root_folder = '淘女郎'
create_folder(root_folder)
url = 'https://mm.taobao.com/json/request_top_list.htm?page=1'
browser = webdriver.PhantomJS()
browser.get(url)
bs = BeautifulSoup(browser.page_source, 'lxml')
for top in bs('p', 'top'):
mm_url = joint_url(top.find('a')['href'])
mm_name = top.find('a').text
mm_age = top.find('em').text
mm_city = top.find('span').text
mm_folder = '%s/%s' % (root_folder, mm_name)
create_folder(mm_folder)
print '发现一位美眉,她叫做%s,今年%s,住在%s,现在开始爬取她的个人页面……' % (mm_name, mm_age, mm_city)
browser.get(mm_url)
bs1 = BeautifulSoup(browser.page_source, 'lxml')
base_info = bs1.find('ul', 'mm-p-info-cell clearfix')
info_list = base_info('span')
result = []
result.append('昵称:' + info_list[0].text)
result.append('生日:' + info_list[1].text.strip())
result.append('所在城市:' + info_list[2].text)
result.append('职业:' + info_list[3].text)
result.append('血型:' + info_list[4].text)
result.append('学校/专业:' + info_list[5].text)
result.append('风格:' + info_list[6].text)
result.append('身高:' + base_info.find('li', 'mm-p-small-cell mm-p-height').find('p').text)
result.append('体重:' + base_info.find('li', 'mm-p-small-cell mm-p-weight').find('p').text)
result.append('三围:' + base_info.find('li', 'mm-p-small-cell mm-p-size').find('p').text)
result.append('罩杯:' + base_info.find('li', 'mm-p-small-cell mm-p-bar').find('p').text)
result.append('鞋码:' + base_info.find('li', 'mm-p-small-cell mm-p-shose').find('p').text)
print '资料收集完毕,正在保存她的个人资料……'
filename = '%s/%s.txt' % (mm_folder, mm_name)
with open(filename, 'w') as f:
f.write('\r\n'.join(result))
print '保存完毕!现在开始爬取她的个人相册……'
album_menu_url = joint_url(bs1.find('ul', 'mm-p-menu').find('a')['href'])
browser.get(album_menu_url)
time.sleep(3)
bs2 = BeautifulSoup(browser.page_source, 'lxml')
album_number = 1
for album_info in bs2('div', 'mm-photo-cell-middle'):
album_url = joint_url(album_info.find('h4').find('a')['href'])
album_name = album_info.find('h4').find('a').text.strip()
album_size = album_info.find('span', 'mm-pic-number').text
print '现在开始爬取她的第%d个相册,相册名为:《%s》%s……' % (album_number, album_name, album_size)
browser.get(album_url)
js1 = 'return document.body.scrollHeight'
js2 = 'window.scrollTo(0, document.body.scrollHeight)'
old_scroll_height = 0
while(browser.execute_script(js1) > old_scroll_height):
old_scroll_height = browser.execute_script(js1)
browser.execute_script(js2)
time.sleep(3)
bs3 = BeautifulSoup(browser.page_source, 'lxml')
photo_number = 1
for photo_area in bs3('div', 'mm-photoimg-area'):
print '现在开始下载她这个相册的第%d张图片……' % photo_number,
photo_url = joint_url(photo_area.find('a')['href'])
browser.get(photo_url)
bs4 = BeautifulSoup(browser.page_source, 'lxml')
big_img_url = joint_url(bs4.find('img', id='J_MmBigImg')['src'])
content = requests.get(big_img_url).content
filename = '%s/%d.jpg' % (mm_folder, photo_number)
with open(filename, 'wb') as f:
f.write(content)
print '下载完毕!'
photo_number += 1
album_number += 1
爬虫实例——爬取淘女郎相册(通过selenium、PhantomJS、BeautifulSoup爬取)的更多相关文章
- 数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置
数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置 2013-05-15 15:08:14 分类: Python/Ruby 数据抓取是一门艺术,和其他软件不同,世界上 ...
- Selenium&PhantomJS 完成爬取网络代理
Selenium模块是一套完整的Web应用程序测试系统,它包含了测试的录制(SeleniumIDE).编写及运行(Selenium Remote Control)和测试的并行处理(Selenimu G ...
- 动态网页爬取例子(WebCollector+selenium+phantomjs)
目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...
- 动态网页爬取样例(WebCollector+selenium+phantomjs)
目标:动态网页爬取 说明:这里的动态网页指几种可能:1)须要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成.如一个html里有<div id="test" ...
- selenium+phantomjs+pyquery 爬取淘宝商品信息
from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium ...
- Python 之selenium+phantomJS斗鱼抓取案例
from selenium import webdriver from bs4 import BeautifulSoup import time if __name__ == '__main__': ...
- scrapy爬虫实例(1)
爬虫实例 对象 阳光问政平台 目标 : 主题,时间,内容 爬取思路 预先设置好items import scrapy class SuperspiderItem(scrapy.Item): title ...
- Selenium+PhantomJS替代方案
问题描述: python3在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium support for PhantomJS has ...
- Selenium+PhantomJS使用时报错原因及解决方案
问题 今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium support for PhantomJS has been dep ...
随机推荐
- 关于Python 中的 if 语句
学习Python,最开始我们都是先从函数学起,Python教程中有很多函数,if算是其中之一. 可能最为人所熟知的编程语句就是 if 语句了.例如: >>> >>> ...
- TW实习日记:第16天
前端的样式bug实在是太太太莫名其妙了,尤其是封装好的组件,一层套一层的,根本不知道是哪一层出了问题...除了改bug就是做新功能,真想吐槽一下这个项目的留言板,根本没人会用吧...这功能实在是太老旧 ...
- lintcode671 循环单词
循环单词 The words are same rotate words if rotate the word to the right by loop, and get another. Cou ...
- The Activation Function in Deep Learning 浅谈深度学习中的激活函数
原文地址:http://www.cnblogs.com/rgvb178/p/6055213.html 版权声明:本文为博主原创文章,未经博主允许不得转载. 激活函数的作用 首先,激活函数不是真的要去激 ...
- POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)
Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...
- 软件工程 part4 评价3作品
作品1 抢答器 地址: https://modao.cc/app/ylGTXobcMU7ePNi6tY53gG4iraLl0md评价: 挺好玩,但是字体大小是个缺陷,简单大方. 作品2:连连看 软件工 ...
- DNS域名解析协议
一. 根域 就是所谓的“.”,其实我们的网址www.baidu.com在配置当中应该是www.baidu.com.(最后有一点),一般我们在浏览器里输入时会省略后面的点,而这也已经成为了习惯. 根域服 ...
- Linux的ll命令详解
ll 列出来的结果详细,有时间,是否可读写等信息 ,象windows里的 详细信息 ls 只列出文件名或目录名 就象windows里的 列表 ll -t 是降序, ll -t | tac 是升序 l ...
- lintcode-13-字符串查找
字符串查找 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 说明 ...
- linux系统中如何进入退出vim编辑器的方法及区别
在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触linux,要修改某个文本文件,不可能像WINDOWS那样操作,更有甚者,进入VI编辑器后,无法退出以致 ...