1、创建Scrapy项目
scrapy startproject Sina
2、进入项目目录,使用命令genspider创建Spider
scrapy genspider sina sina.com.cn
3、定义要抓取的数据(处理items.py文件)
# -*- coding: utf-8 -*-
# 爬取新浪网分类资讯
# 爬取新浪网导航页下所有大类、小类、小类里的子链接,以及子链接页面的新闻内容。
import scrapy
class SinaItem(scrapy.Item):
# 大类的标题和url
parentTitle = scrapy.Field()
parentUrls = scrapy.Field()
# 小类的标题和子url
subTitle = scrapy.Field()
subUrls = scrapy.Field()
# 小类的目录存储路径
subFilename = scrapy.Field()
# 小类下的子链接
sonUrls = scrapy.Field()
# 文章的标题和内容
head = scrapy.Field()
content = scrapy.Field()
4、编写提取item数据的Spider(在spiders文件夹下:sina.py)
# -*- coding: utf-8 -*-
import scrapy
import os
from Sina.items import SinaItem
class SinaSpider(scrapy.Spider):
name = 'sina'
allowed_domains = ['sina.com.cn']
start_urls = ['http://news.sina.com.cn/guide/']
def parse(self, response):
items = []
# 所有大类的url和标题
parentUrls = response.xpath('//div[@id="tab01"]/div/h3/a/@href').extract()
parentTitle = response.xpath('//div[@id="tab01"]/div/h3/a/text()').extract()
# 所有小类的url和标题
subUrls = response.xpath('//div[@id="tab01"]/div/ul/li/a/@href').extract()
subTitle = response.xpath('//div[@id="tab01"]/div/ul/li/a/text()').extract()
# 获取所有大类
for i in range(0,len(parentTitle)):
# 指定大类目录路径和目录名
parentFilename = './Data/' + parentTitle[i]
# 如果目录不存在则创建目录
if(not os.path.exists(parentFilename)):
os.makedirs(parentFilename)
# 获取所有小类
for j in range(0, len(subUrls)):
item = SinaItem()
# 保存大类的title和urls
item['parentTitle'] = parentTitle[i]
item['parentUrls'] = parentUrls[i]
# 检查小类的url是否以同类别大类url开头,如果是返回True (sports.sina.com.cn 和 sports.sina.com.cn/nba)
# 关于startswith()介绍参考:http://www.cdsy.xyz/python3/python3-string-startswith.html
if_belong = subUrls[j].startswith(item['parentUrls'])
# 如果属于本大类,将小类存储目录放在本大类的目录下
if (if_belong):
subFilename = parentFilename + '/' + subTitle[j]
# 如果目录不存在,则创建
if(not os.path.exists(subFilename)):
os.makedirs(subFilename)
# 保存小类的url、title和filename字段数据
item['subUrls'] = subUrls[j]
item['subTitle'] = subTitle[j]
item['subFilename'] = subFilename
items.append(item)
# 发送每个小类url的Request请求,得到Response连同包含meta数据一同交给回调函数second_parse方法处理
for item in items:
yield scrapy.Request(url = item['subUrls'], meta={'meta_1':item},callback=self.second_parse)
# 对于返回的小类url,再进行递归请求
def second_parse(self,response):
# 提取每次response的meta数据
meta_1 = response.meta['meta_1']
# 取出小类里所有子链接
sonUrls = response.xpath('//a/@href').extract()
items = []
for i in range(0,len(sonUrls)):
# 检查每个链接是否以大类url开头、以.shtml结尾,如果是返回True
if_belong = sonUrls[i].endswith('.shtml') and sonUrls[i].startswith(meta_1['parentUrls'])
# 如果属于本大类,获取字段值放在同一个item下便于传输
if(if_belong):
item = SinaItem()
item['parentTitle'] = meta_1['parentTitle']
item['parentUrls'] = meta_1['parentUrls']
item['subTitle'] = meta_1['subTitle']
item['subUrls'] = meta_1['subUrls']
item['subFilename'] = meta_1['subFilename']
item['sonUrls'] = sonUrls[i]
items.append(item)
# 发送每个小类下子链接url的Request请求,得到Response后连同包含meta数据一同交给回调函数detail_parse方法处理
for item in items:
yield scrapy.Request(url=item['sonUrls'],meta={'meta_2':item},callback=self.detail_parse)
# 数据解析方法,获取文章标题和内容
def detail_parse(self,response):
item = response.meta['meta_2']
content = ''
# 文章标题
head = response.xpath(('//h1[@class="main-title"]/text()')).extract()
# 文章内容,多个p标签组成的列表
content_list = response.xpath('//div[@class="article"]/p/text()').extract()
# 需要将p标签内容拼接在一起
for content_one in content_list:
content += content_one
item['head'] = head
item['content'] = content
yield item
5、处理pipelines管道文件保存数据,可将结果保存到文件中(pipelines.py)
# -*- coding: utf-8 -*-
class SinaPipeline(object):
def process_item(self, item, spider):
sonUrls = item['sonUrls']
# 文件名为子链接url中间部分,并将 / 替换为 _,保存为 .txt格式
filename = sonUrls[7:-6].replace("/","_")
filename += ".txt"
with open(item['subFilename']+ "/" + filename,'w',encoding='utf-8')as f:
f.write(item['content'])
return item
6、配置settings文件(settings.py)
# Obey robots.txt rules,具体含义参照:https://www.cdsy.xyz/computer/programme/Python/241210/cd64912.html
ROBOTSTXT_OBEY = False
# 下载延迟
DOWNLOAD_DELAY = 2
# Override the default request headers:添加User-Agent信息
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',
}
# Configure item pipelines去掉下面注释,打开管道文件
ITEM_PIPELINES = {
'Sina.pipelines.SinaPipeline': 300,
}
# 还可以将日志存到本地文件中(可选添加设置)
LOG_FILE = "sina.log"
LOG_LEVEL = "DEBUG"
# 包含打印信息也一起写进日志里
LOG_STDOUT = True
7.以上设置完毕,进行爬取:执行项目命令crawl,启动Spider:
scrapy crawl sina