javaraylu 2019-11-02
安装
pip install scrapy
建立一个爬虫项目
scrapy startproject 项目名称
scrapy startproject itcast
生成一个爬虫
scrapy genspider 爬虫名称 "爬虫范围"
scrapy genspider itcast "itcast.cn"
爬虫生成位置

编写itcast.py
# -*- coding: utf-8 -*-
import scrapy
class ItcastSpider(scrapy.Spider):
name = "itcast"
allowed_domains = ["itcast.cn"]
start_urls = (
‘http://www.itcast.cn/channel/teacher.shtml‘,
)
def parse(self, response):
# print(response)
data_list = response.xpath("//div[@class=‘tea_con‘]//h3/text()").extract() # extract() 返回一个含有字符串数据的列表 如果没用这个方法 返回一个包含选择器的列表
print(data_list) # 乱码 u\u5218.... setting.py中添加了 FEED_EXPORT_ENCODING = ‘utf-8‘ 还是不行 不知道原因 ???
for i in data_list:
print(i) # 此处打印的是中文乱码是由于ubuntu终端没有中文安装包
安装中文包
apt-get install language-pack-zh
修改 /tec/environment
sudo gedit /etc/environment
在下面添加两行
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" LANG="zh_CN.UTF-8" LANGUAGE="zh_CN:zh:en_US:en"
第二行即是默认的中文字符编码。注:可以通过这里修改默认的中文编 码字符,比如修改为:zh_CN.GBK
修改/var/lib/locales/supported.d/local文件
sudo gedit /var/lib/locales/supported.d/local
添加
zh_CN.UTF-8 UTF-8 en_US.UTF-8 UTF-8
保存后,执行命令
sudo locale-gen
重启
sudo reboot
解决 乱码没有了,可以显示中文了
终端打印出来后有其它数据
setting.py中配置log的等级
LOG_LEVEL = "WARNING"
xapath分组 数据传到pipline itcast.py中
# -*- coding: utf-8 -*-
import scrapy
class ItcastSpider(scrapy.Spider):
name = "itcast"
allowed_domains = ["itcast.cn"]
start_urls = (
‘http://www.itcast.cn/channel/teacher.shtml‘,
)
def parse(self, response):
# # print(response)
# data_list = response.xpath("//div[@class=‘tea_con‘]//h3/text()").extract() # extract() 返回一个含有字符串数据的列表 如果没用这个方法 返回一个包含选择器的列表
# print(data_list) # 乱码 u\u5218.... setting.py中添加了 FEED_EXPORT_ENCODING = ‘utf-8‘ 还是不行 不知道原因 ???
# for i in data_list:
# print(i) # 此处打印的是中文
ret = response.xpath("//div[@class=‘tea_con‘]//li") # xpath分组提取
# print(ret)
for i in ret:
item = {}
item[‘name‘] = i.xpath(".//h3/text()").extract_first() # extract_first()相当于 extract()[0] 取列表的第一条数据
# extrack_first() 如果没有数据则返回空列表
# extrack()[0] 如果没有数据会报错
item[‘position‘] = i.xpath(".//h4/text()").extract_first()
item[‘commondcommond‘] = i.xpath(".//p/text()").extract_first()
yield item # 把数据传给piplinepipline如果想显示接收数据 先要在设置setting.py中开启

# -*- coding:utf-8 -*-
# Define your item pipelines here
#
# Don‘t forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import codecs
class MyspiderPipeline(object):
# def __init__(self):
# # 定义文件编码及名称
# self.file = codecs.open(‘中文乱码.json‘, ‘wb‘, encoding=‘utf-8‘)
def process_item(self, item, spider): # 实现存储方法
# line = json.dumps(dict(item)) + ‘\n‘
# print(line.decode("unicode_escape"))
# 写入一行,每行为一个抓取项
# self.file.write(line.decode("unicode_escape"))
# return item
print(item) return item查看效果,控制端输入代码
scrapy crawl itcast


# -*- coding:utf-8 -*-
# Define your item pipelines here
#
# Don‘t forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import codecs
class MyspiderPipeline(object):
# def __init__(self):
# # 定义文件编码及名称
# self.file = codecs.open(‘中文乱码.json‘, ‘wb‘, encoding=‘utf-8‘)
def process_item(self, item, spider):
# line = json.dumps(dict(item)) + ‘\n‘
# print(line.decode("unicode_escape"))
# 写入一行,每行为一个抓取项
# self.file.write(line.decode("unicode_escape"))
# return item
del item["commondcommond"] # 删除详细介绍
return item
class MyspiderPipeline2(object):
def process_item(self, item, spider):
print(item) # 此时item是从上面方法处理后的item
return item配置setting.py
# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
‘myspider.pipelines.MyspiderPipeline‘: 300,
‘myspider.pipelines.MyspiderPipeline2‘: 301,
}查看效果
