制作scrapy爬虫一共需要4步:
1.新建项目(scrapy startproject xxx):新建一个新的爬虫项目
2.明确目标(编写items.py):明确你想要抓取的目标
3.制作爬虫(xxspider.py):制作爬虫开始爬取网页
4.存储内容(pipelines.py):设计管道存储爬取内容
爬虫顺序:main.py--->items/py-->xxspider.py-->settings.py-->pipelines.py
main.py是用于运行爬虫代码的,一般格式为:
from scrapy.cmdline import execute
import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# execute(['scrapy','crawl','sun'])
execute(['scrapy','crawl','你的爬虫名'])
若你爬取了图片,可在settings.py里自定义设置图片保存路径
在管道文件里可获取setting信息
在提crawlspider之前说一下正则表达式的有个方法
re.sub(s1,s2,s3) 将s3里所有s1替换成s2
和自增10一样
Crawlspider
通过scrapy genspider -t crawl xxx xxxxxx.com可快速创建crawlspider模块的代码
在xxx.py文件里要导入:
1.
from scrapy.linkextractors import LinkExtractor
就是取网页里的链接
2
from scrapy.spiders import CrawlSpider, Rule
调用里面的两个函数来处理链接
3.实现
#Rule和LinkExtractor放到一起写了.allow是匹配符合的,deny是匹配出不符合的,没有回调函数follow=True,就跟进链接 rules = ( #跟进第一页,第二页... Rule(LinkExtractor(allow=r'type=4&page=\d+')), #有回调函数follow=False,就不跟进,利用回调函数来处理.这里的回调函数不能加(),必须得用双引号引起来 Rule(LinkExtractor(allow=r'/html/question/\d+/\d+.shtml'), callback = 'parse_item'), )
注意用crawlspider后解析函数就不能命名为parse,用其他任何的代替都行
response.url就可以取网址 follow=True 跟进
allow是匹配符合的,deny是匹配出不符合的
有回调函数follow=False,就不跟进,利用回调函数来处理.这里的回调函数不能加(),必须得用双引号引起来
没有回调函数follow=True,就跟进链接
Rule自带process_links方法,用来过滤数据 , deal_links则是用来过滤数据的函数,要自定义
Rule(pagelink, process_links = "deal_links")
付一个爬取阳光热线问政平台的代码
main:
from scrapy.cmdline import execute
import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# execute(['scrapy','crawl','sun'])
execute(['scrapy','crawl','dongdong'])
items:
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class DongguanItem(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
content = scrapy.Field()
url = scrapy.Field()
number = scrapy.Field()
xxx:
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from dongguan.items import DongguanItem
#爬整栈用到crawlspider 注意解析网页
class DongdongSpider(CrawlSpider):
name = 'dongdong'
allowed_domains = ['wz.sun0769.com']
start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4&page=']
# 每一页的匹配规则
pagelink = LinkExtractor(allow=("type=4"))
# 每一页里的每个帖子的匹配规则
contentlink = LinkExtractor(allow=(r"/html/question/\d+/\d+.shtml"))
rules = (
# url被web服务器篡改,需要调用process_links来处理提取出来的url
#process_links是自带 的方法,用来过滤数据
Rule(pagelink, process_links = "deal_links"),
Rule(contentlink, callback = "parse_item")
)
# links 是当前response里提取出来的链接列表
def deal_links(self, links):
for each in links:
each.url = each.url.replace("?","&").replace("Type&","Type?")
return links
def parse_item(self, response):
item = DongguanItem()
# 标题
item['title'] = response.xpath('//div[contains(@class, "pagecenter p3")]//strong/text()').extract()[0]
# 编号
item['number'] = item['title'].split(' ')[-1].split(":")[-1]
# 内容,先使用有图片情况下的匹配规则,如果有内容,返回所有内容的列表集合
content = response.xpath('//div[@class="contentext"]/text()').extract()
# 如果没有内容,则返回空列表,则使用无图片情况下的匹配规则
if len(content) == 0:
content = response.xpath('//div[@class="c1 text14_2"]/text()').extract()
item['content'] = "".join(content).strip()
else:
item['content'] = "".join(content).strip()
# 链接
item['url'] = response.url
yield item
setting:
# -*- coding: utf-8 -*-
# Scrapy settings for dongguan project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://doc.scrapy.org/en/latest/topics/settings.html
# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'dongguan'
SPIDER_MODULES = ['dongguan.spiders']
NEWSPIDER_MODULE = 'dongguan.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'dongguan (+http://www.yourdomain.com)'
# Obey robots.txt rules
# ROBOTSTXT_OBEY = True
# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32
# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
# Disable cookies (enabled by default)
#COOKIES_ENABLED = False
# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False
# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);'
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
}
# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'dongguan.middlewares.DongguanSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'dongguan.middlewares.DongguanDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'dongguan.pipelines.DongguanPipeline': 300,
}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False
# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
pipelines:
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
class DongguanPipeline(object):
def __init__(self):
self.filename = open("dongguan.json", "wb+")
def process_item(self, item, spider):
text = json.dumps(dict(item), ensure_ascii = False) + ",\n"
self.filename.write(text.encode("utf-8"))
return item
def close_spider(self, spider):
self.filename.close()