scrapy从安装到爬取煎蛋网图片
下载地址: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从安装到爬取煎蛋网图片的更多相关文章
- Python 爬虫 爬取 煎蛋网 图片
今天, 试着爬取了煎蛋网的图片. 用到的包: urllib.request os 分别使用几个函数,来控制下载的图片的页数,获取图片的网页,获取网页页数以及保存图片到本地.过程简单清晰明了 直接上源代 ...
- python爬取煎蛋网图片
``` py2版本: #-*- coding:utf-8 -*-#from __future__ import unicode_literimport urllib,urllib2,timeimpor ...
- Python Scrapy 爬取煎蛋网妹子图实例(一)
前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...
- selenium爬取煎蛋网
selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...
- python3爬虫爬取煎蛋网妹纸图片(上篇)
其实之前实现过这个功能,是使用selenium模拟浏览器页面点击来完成的,但是效率实际上相对来说较低.本次以解密参数来完成爬取的过程. 首先打开煎蛋网http://jandan.net/ooxx,查看 ...
- python爬虫–爬取煎蛋网妹子图片
前几天刚学了python网络编程,书里没什么实践项目,只好到网上找点东西做. 一直对爬虫很好奇,所以不妨从爬虫先入手吧. Python版本:3.6 这是我看的教程:Python - Jack -Cui ...
- 爬虫实例——爬取煎蛋网OOXX频道(反反爬虫——伪装成浏览器)
煎蛋网在反爬虫方面做了不少工作,无法通过正常的方式爬取,比如用下面这段代码爬取无法得到我们想要的源代码. import requests url = 'http://jandan.net/ooxx' ...
- Python 爬取煎蛋网妹子图片
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...
- Scrapy爬虫框架之爬取校花网图片
Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...
随机推荐
- tapable事件流插件
tapable Webpack本质上是一种事件流的机制,它的工作流程就是将各个插件串联起来,而实现这一切的核心就是Tapable,webpack中最核心的负责编译的Compiler和负责创建bundl ...
- COGS2294 释迦
传送门 就是传说中的任意模数卷积嘛……有三模数NTT和拆系数FFT等做法,我比较懒不想动脑子,就用了三模数NTT的做法…… 卷积之后每个数可以达到$10^{23}$左右的级别,直接long doubl ...
- Flex和MyEclipse10整合时候需要注意的问题
1.myeclipse和flex的位数要一致,不能混着安装 2.独立安装完Adobe Flash Builder 4.6 Installer之后,在其的安装文件夹下有一个utilities文件夹下有一 ...
- Jave 之方法-函数(5)
如何定义Java中的方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. (方法在C语言中被称为函数) 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问 ...
- Android之自定义View以及画一个时钟
https://www.2cto.com/kf/201509/443112.html 概述: 当Android自带的View满足不了开发者时,自定义View就发挥了很好的作用.建立一个自定义View, ...
- 如何判断单链表是否存在环 & 判断两链表是否相交
给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...
- 实现UILabel渐变色效果
实现UILabel渐变色效果 效果如下图: 源码: // // CombinationView.h // ChangeColorLabel // // Created by YouXianMing o ...
- css笔记 2
定义一个类选择器.center {text-align: center} h1 有 center 类.这意味将遵守 ".center" 选择器中的规则.<h1 class=& ...
- 如何跟踪比特币SV(BSV)地址余额?
Coin Balance Tracker 能够查看任何BSV/BCH/BTC地址的当前余额.您只需要确保你的电脑能连接Internet以及地址正确.此程序非常安全,因为它只使用公共数据,不需要登录和密 ...
- 对EJB的认识
对EJB的认识 接触EJB以来有一段时间了,走马观花一样把它所涉及到的东西看了一遍,随着深入了解越来越感觉到ejb的很强大,用了java后觉的java好用.学历SSH觉的比java好用.学了ejb觉的 ...