下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/
pip install wheel
pip install lxml
pip install pyopenssl
pip install Twisted
pip install pywin32
pip install scrapy

scrapy startproject jandan 创建项目
cd jandan
cd jandan

items.py 存放数据
pipelines.py 管道文件

由于煎蛋网有反爬虫措施,我们需要做一些处理

settings文件

ROBOTSTXT_OBEY = False #不遵寻reboot协议
DOWNLOAD_DELAY = 2 #下载延迟时间
DOWNLOAD_TIMEOUT = 15 #下载超时时间
COOKIES_ENABLED = False #禁用cookie
DOWNLOADER_MIDDLEWARES = {
#请求头
'jandan.middlewares.RandomUserAgent': 100,
#代理ip
'jandan.middlewares.RandomProxy': 200,
}
#请求列表
USER_AGENTS = [
#遨游
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
#火狐
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
#谷歌
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
] #代理ip列表
PROXIES = [
{"ip_port":"119.177.90.103:9999","user_passwd":""},
#代理ip无密码
{"ip_port":"101.132.122.230:3128","user_passwd":""},
#代理ip有密码
# {"ip_port":"123.139.56.238:9999","user_passwd":"root:admin"}
]
#管道文件,取消注释
ITEM_PIPELINES = {
'jandan.pipelines.JandanPipeline': 300,
}
IMAGES_STORE = "images"
middlewares文件
import random
import base64
from jandan.settings import USER_AGENTS
from jandan.settings import PROXIES class RandomUserAgent(object):
def process_request(self,request,spider):
useragent = random.choice(USER_AGENTS)
request.headers.setdefault("User-Agent",useragent) class RandomProxy(object):
def process_request(self,request,spider):
proxy = random.choice(PROXIES)
if proxy["user_passwd"] is None:
request.meta["proxy"] = "http://" + proxy["ip_port"]
else:
# b64编码接收字节对象,在py3中str是unicode,需要转换,返回是字节对象
base64_userpasswd = base64.b16encode(proxy["user_passwd"].encode())
request.meta["proxy"] = "http://" + proxy["ip_port"]
#拼接是字符串,需要转码
request.headers["Proxy-Authorization"] = "Basic " + base64_userpasswd.decode()

items文件

import scrapy

class JandanItem(scrapy.Item):
name = scrapy.Field()
url = scrapy.Field()

scrapy genspider  -t crawl dj jandan.net 创建crawlscrapy类爬虫
会自动在spiders下创建jandan.py文件,页面由js编写,需要BeautifulSoup类定位js元素获取数据

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from jandan.items import JandanItem
from selenium import webdriver
from bs4 import BeautifulSoup as bs4 class JdSpider(CrawlSpider):
name = 'jd'
allowed_domains = ['jandan.net']
start_urls = ['http://jandan.net/pic/page-1#comments/'] rules = (
Rule(LinkExtractor(allow=r'pic/page-\d+'), callback='parse_item', follow=True),
) def parse_item(self, response):
item = JandanItem()
driver = webdriver.PhantomJS()
driver.get(response.url)
soup = bs4(driver.page_source, 'html.parser')
all_data = soup.find_all('div', {'class': 'row'})
for i in all_data:
name = i.find("strong")
item["name"] = name.get_text().strip()
link = i.find('a', {'class': 'view_img_link'})
url = link.get("href")
if len(url) == 0:
return
item["url"] = "http://" + url.split("//")[-1]
yield item

pipelines.py

import json
import os
import requests
from scrapy.conf import settings class JandanPipeline(object):
   #保存为json文件
# def __init__(self):
# self.filename = open("jandan.json","wb")
# self.num = 0
#
# def process_item(self, item, spider):
# text = json.dumps(dict(item),ensure_ascii=False) + "\n"
# self.filename.write(text.encode("utf-8"))
# self.num += 1
# return item
#
# def close_spider(self,spider):
# self.filename.close()
# print("总共有" + str(self.num) + "个资源")   #下载到本地
def process_item(self, item, spider):
if 'url' in item:
dir_path = settings["IMAGES_STORE"]
if not os.path.exists(dir_path):
os.makedirs(dir_path)
su = "." + item["url"].split(".")[-1]
path = item["name"] + su
new_path = '%s/%s' % (dir_path, path)
if not os.path.exists(new_path):
with open(new_path, 'wb') as handle:
response = requests.get(item["url"], stream=True)
for block in response.iter_content(1024):
if not block:
break handle.write(block)
return item

scrapy crawl dj 启动爬虫

scrapy shell "https://hr.tencent.com/position.php?&start=0" 发送请求

奉上我的github地址,会定期更新项目

https://github.com/bjptw/workspace

scrapy从安装到爬取煎蛋网图片的更多相关文章

  1. Python 爬虫 爬取 煎蛋网 图片

    今天, 试着爬取了煎蛋网的图片. 用到的包: urllib.request os 分别使用几个函数,来控制下载的图片的页数,获取图片的网页,获取网页页数以及保存图片到本地.过程简单清晰明了 直接上源代 ...

  2. python爬取煎蛋网图片

    ``` py2版本: #-*- coding:utf-8 -*-#from __future__ import unicode_literimport urllib,urllib2,timeimpor ...

  3. Python Scrapy 爬取煎蛋网妹子图实例(一)

    前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...

  4. selenium爬取煎蛋网

    selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...

  5. python3爬虫爬取煎蛋网妹纸图片(上篇)

    其实之前实现过这个功能,是使用selenium模拟浏览器页面点击来完成的,但是效率实际上相对来说较低.本次以解密参数来完成爬取的过程. 首先打开煎蛋网http://jandan.net/ooxx,查看 ...

  6. python爬虫–爬取煎蛋网妹子图片

    前几天刚学了python网络编程,书里没什么实践项目,只好到网上找点东西做. 一直对爬虫很好奇,所以不妨从爬虫先入手吧. Python版本:3.6 这是我看的教程:Python - Jack -Cui ...

  7. 爬虫实例——爬取煎蛋网OOXX频道(反反爬虫——伪装成浏览器)

    煎蛋网在反爬虫方面做了不少工作,无法通过正常的方式爬取,比如用下面这段代码爬取无法得到我们想要的源代码. import requests url = 'http://jandan.net/ooxx' ...

  8. Python 爬取煎蛋网妹子图片

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...

  9. Scrapy爬虫框架之爬取校花网图片

    Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...

随机推荐

  1. mysql启动服务出错--发生系统错误 1067。

    记以此安装mysql出错的问题,MySQL 服务无法启动.系统出错  发生系统错误 1067.进程意外终止. 今天在安装本地mysql是,使用net start mysql命令启动服务时,总是报106 ...

  2. JavaScript总结摘要

    一 概述 1.什么是JavaScript? 基于对象.由事件驱动的解释性脚本语言. 2.JavaScript语法特点 区分大写小,这一点不同于HTML. 结尾的分号可有可无. 变量是弱类型的:变量在定 ...

  3. 01_Mac下安装homebrew

    参考:https://jingyan.baidu.com/album/fec7a1e5ec30341190b4e7e5.html?picindex=3 1.在打开的命令行工具中输入如下语句: ruby ...

  4. 【Web crawler】print_all_links

    How to repeat Procedures&Control CS重要概念 1.1 过程procedures 封装代码,代码重用 1.2 控制Control DEMO # -*- codi ...

  5. 微信小程序——小程序的能力

    小程序启动 通过app.json里pages字段可以获得页面路径,而写在 pages 字段的第一个页面就是这个小程序的首页(打开小程序看到的第一个页面),就像下面的代码中,小程序启动后的第一个页面就是 ...

  6. c# 修改winform中app.config的配置值

    public bool ChangeConfig(string AppKey,string AppValue) { bool result = true; try { XmlDocument xDoc ...

  7. 关于派生类访问基类对象的保护变量的问题 --Coursera

    https://class.coursera.org/pkupop-001/forum/thread?thread_id=350   郭天魁· 6 months ago 在课件中我们知道如下程序是不能 ...

  8. excel操作方法

    excel分列: http://jingyan.baidu.com/article/54b6b9c0d53f622d593b4772.html excel分列: http://jingyan.baid ...

  9. 阅读MySQL文档第20章:存储程序和函数

    本文把阅读到的重点摘抄下来. 一.一个子程序要么是一个程序要么是一个函数.使用CALL语句来调用程序,程序只能用输出变量传回值.就像别其它函数调用一样,函数可以被从语句外调用(即通过引用函数名),函数 ...

  10. JavaScript 初学备忘录

    JavaScript 是脚本语言 JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编程代码. JavaScript 插入 HTML 页面后,可由所有的现 ...