【转载】Scrapy安装及demo测试笔记
Scrapy安装及demo测试笔记
Scrapy安装及demo测试笔记
一、环境搭建
1. 安装scrapy:pip install scrapy
2.安装:PyWin32,可以从网上载已编译好的安装包:http://www.lfd.uci.edu/%7Egohlke/pythonlibs/#pywin32
安装完之后会报如下错误
解决办法,把以下两个文件拷贝到C:\Windows\System32目录下
二、创建scrapy工程(在此用网上别人提供的例子)
1.cmd的方式进到某个指定目录(d:/tmp/)下执行:scrapy startproject myscrapy,命令执行完之后,生成的目录结构如下
2.设置items
- # -*- coding: utf-8 -*-
- # Define here the models for your scraped items
- #
- # See documentation in:
- # http://doc.scrapy.org/en/latest/topics/items.html
- import scrapy
- class MyscrapyItem(scrapy.Item):
- news_title = scrapy.Field() #南邮新闻标题
- news_date = scrapy.Field() #南邮新闻时间
- news_url = scrapy.Field() #南邮新闻的详细链接
3.编写 spider
- # -*- coding: utf-8 -*-
- import scrapy
- from myscrapy.items import MyscrapyItem
- import logging
- class myscrapySpider(scrapy.Spider):
- name = "myscrapy"
- allowed_domains = ["njupt.edu.cn"]
- start_urls = [
- "http://news.njupt.edu.cn/s/222/t/1100/p/1/c/6866/i/1/list.htm",
- ]
- def parse(self, response):
- news_page_num = 14
- page_num = 386
- if response.status == 200:
- for i in range(2,page_num+1):
- for j in range(1,news_page_num+1):
- item = MyscrapyItem()
- item['news_url'],item['news_title'],item['news_date'] = response.xpath(
- "//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/font/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//td[@class='postTime']/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/@href").extract()
- yield item
- next_page_url = "http://news.njupt.edu.cn/s/222/t/1100/p/1/c/6866/i/"+str(i)+"/list.htm"
- yield scrapy.Request(next_page_url,callback=self.parse_news)
- def parse_news(self, response):
- news_page_num = 14
- if response.status == 200:
- for j in range(1,news_page_num+1):
- item = MyscrapyItem()
- item['news_url'],item['news_title'],item['news_date'] = response.xpath(
- "//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/font/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//td[@class='postTime']/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/@href").extract()
- yield item
4.编写pipelines
- # -*- 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
- class MyscrapyPipeline(object):
- def __init__(self):
- self.file = open('myscrapy.txt',mode='wb')
- def process_item(self, item, spider):
- self.file.write(item['news_title'].encode("GBK"))
- self.file.write("\n")
- self.file.write(item['news_date'].encode("GBK"))
- self.file.write("\n")
- self.file.write(item['news_url'].encode("GBK"))
- self.file.write("\n")
- return item
5.编写settings.py
- # -*- coding: utf-8 -*-
- # Scrapy settings for myscrapy project
- #
- # For simplicity, this file contains only settings considered important or
- # commonly used. You can find more settings consulting the documentation:
- #
- # http://doc.scrapy.org/en/latest/topics/settings.html
- # http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
- # http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
- BOT_NAME = 'myscrapy'
- SPIDER_MODULES = ['myscrapy.spiders']
- NEWSPIDER_MODULE = 'myscrapy.spiders'
- # Crawl responsibly by identifying yourself (and your website) on the user-agent
- #USER_AGENT = 'myscrapy (+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 http://scrapy.readthedocs.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 = {
- # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- # 'Accept-Language': 'en',
- #}
- # Enable or disable spider middlewares
- # See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
- #SPIDER_MIDDLEWARES = {
- # 'myscrapy.middlewares.MyCustomSpiderMiddleware': 543,
- #}
- # Enable or disable downloader middlewares
- # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
- #DOWNLOADER_MIDDLEWARES = {
- # 'myscrapy.middlewares.MyCustomDownloaderMiddleware': 543,
- #}
- # Enable or disable extensions
- # See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
- #EXTENSIONS = {
- # 'scrapy.extensions.telnet.TelnetConsole': None,
- #}
- # Configure item pipelines
- # See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
- ITEM_PIPELINES = {
- 'myscrapy.pipelines.MyscrapyPipeline': 1,
- }
- # Enable and configure the AutoThrottle extension (disabled by default)
- # See http://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 http://scrapy.readthedocs.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'
6.进到D:\tmp\myscrapy\myscrapy\spiders启动爬虫并查看结果:scrapy crawl myscrapy
Scrapy安装及demo测试笔记
Scrapy安装及demo测试笔记
一、环境搭建
1. 安装scrapy:pip install scrapy
2.安装:PyWin32,可以从网上载已编译好的安装包:http://www.lfd.uci.edu/%7Egohlke/pythonlibs/#pywin32
安装完之后会报如下错误
解决办法,把以下两个文件拷贝到C:\Windows\System32目录下
二、创建scrapy工程(在此用网上别人提供的例子)
1.cmd的方式进到某个指定目录(d:/tmp/)下执行:scrapy startproject myscrapy,命令执行完之后,生成的目录结构如下
2.设置items
- # -*- coding: utf-8 -*-
- # Define here the models for your scraped items
- #
- # See documentation in:
- # http://doc.scrapy.org/en/latest/topics/items.html
- import scrapy
- class MyscrapyItem(scrapy.Item):
- news_title = scrapy.Field() #南邮新闻标题
- news_date = scrapy.Field() #南邮新闻时间
- news_url = scrapy.Field() #南邮新闻的详细链接
3.编写 spider
- # -*- coding: utf-8 -*-
- import scrapy
- from myscrapy.items import MyscrapyItem
- import logging
- class myscrapySpider(scrapy.Spider):
- name = "myscrapy"
- allowed_domains = ["njupt.edu.cn"]
- start_urls = [
- "http://news.njupt.edu.cn/s/222/t/1100/p/1/c/6866/i/1/list.htm",
- ]
- def parse(self, response):
- news_page_num = 14
- page_num = 386
- if response.status == 200:
- for i in range(2,page_num+1):
- for j in range(1,news_page_num+1):
- item = MyscrapyItem()
- item['news_url'],item['news_title'],item['news_date'] = response.xpath(
- "//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/font/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//td[@class='postTime']/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/@href").extract()
- yield item
- next_page_url = "http://news.njupt.edu.cn/s/222/t/1100/p/1/c/6866/i/"+str(i)+"/list.htm"
- yield scrapy.Request(next_page_url,callback=self.parse_news)
- def parse_news(self, response):
- news_page_num = 14
- if response.status == 200:
- for j in range(1,news_page_num+1):
- item = MyscrapyItem()
- item['news_url'],item['news_title'],item['news_date'] = response.xpath(
- "//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/font/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//td[@class='postTime']/text()"
- "|//div[@id='newslist']/table[1]/tr["+str(j)+"]//a/@href").extract()
- yield item
4.编写pipelines
- # -*- 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
- class MyscrapyPipeline(object):
- def __init__(self):
- self.file = open('myscrapy.txt',mode='wb')
- def process_item(self, item, spider):
- self.file.write(item['news_title'].encode("GBK"))
- self.file.write("\n")
- self.file.write(item['news_date'].encode("GBK"))
- self.file.write("\n")
- self.file.write(item['news_url'].encode("GBK"))
- self.file.write("\n")
- return item
5.编写settings.py
- # -*- coding: utf-8 -*-
- # Scrapy settings for myscrapy project
- #
- # For simplicity, this file contains only settings considered important or
- # commonly used. You can find more settings consulting the documentation:
- #
- # http://doc.scrapy.org/en/latest/topics/settings.html
- # http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
- # http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
- BOT_NAME = 'myscrapy'
- SPIDER_MODULES = ['myscrapy.spiders']
- NEWSPIDER_MODULE = 'myscrapy.spiders'
- # Crawl responsibly by identifying yourself (and your website) on the user-agent
- #USER_AGENT = 'myscrapy (+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 http://scrapy.readthedocs.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 = {
- # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
- # 'Accept-Language': 'en',
- #}
- # Enable or disable spider middlewares
- # See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
- #SPIDER_MIDDLEWARES = {
- # 'myscrapy.middlewares.MyCustomSpiderMiddleware': 543,
- #}
- # Enable or disable downloader middlewares
- # See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
- #DOWNLOADER_MIDDLEWARES = {
- # 'myscrapy.middlewares.MyCustomDownloaderMiddleware': 543,
- #}
- # Enable or disable extensions
- # See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
- #EXTENSIONS = {
- # 'scrapy.extensions.telnet.TelnetConsole': None,
- #}
- # Configure item pipelines
- # See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
- ITEM_PIPELINES = {
- 'myscrapy.pipelines.MyscrapyPipeline': 1,
- }
- # Enable and configure the AutoThrottle extension (disabled by default)
- # See http://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 http://scrapy.readthedocs.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'
6.进到D:\tmp\myscrapy\myscrapy\spiders启动爬虫并查看结果:scrapy crawl myscrapy
【转载】Scrapy安装及demo测试笔记的更多相关文章
- red5研究(一):下载,工程建立、oflaDemo安装、demo测试
一.red5下载.添加工程到myeclipse 1,从官网上下载red51.01版本(我下载的是red51.0的版本),下载链接http://www.red5.org/downloads/red5/1 ...
- hue安装及基本测试-笔记
#################################################################################################### ...
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
- Python、pip和scrapy的安装——Python爬虫学习笔记1
Python作为爬虫语言非常受欢迎,近期项目需要,很是学习了一番Python,在此记录学习过程:首先因为是初学,而且当时要求很快速的出demo,所以首先想到的是框架,一番查找选用了Python界大名鼎 ...
- Python3 Scrapy 安装方法
Python3 Scrapy 安装方法 (一脸辛酸泪) 写在前面 最近在学习爬虫,在熟悉了Python语言和BeautifulSoup4后打算下个爬虫框架试试. 没想到啊,这坑太深了... 看了看相关 ...
- 02 Linux 下安装JDK并测试开发“Hello World!”
测试环境 主机系统:Win7 64位 虚拟机:VMware® Workstation 11.1.0 虚拟机系统:CentOS 6.5 64位 Kernel 2.6.32-431.e16.x86_6 ...
- Python实用工具包Scrapy安装教程
对于想用每个想用Python开发网络爬虫的开发者来说,Scrapy无疑是一个极好的开源工具.今天安装之后觉得Scrapy的安装确实不易啊.所以在此博文一篇,往后来着少走弯路. 废话不多说了,如果 ...
- Chapter 2. OpenSSL的安装和配置学习笔记
Chapter 2. OpenSSL的安装和配置学习笔记 2.1 在linux上面安装OpenSSL我还是做点No paper事情比较在行,正好和老师的课程接轨一下.以前尝试过在Windows上面安装 ...
- 中标麒麟6.0_ICE3.4.2编译+demo测试(CPP)
(菜鸟版)确保 gcc版本4.4.6(其他版本未测试),4.8不行 一.降级GCC到4.4.6 注意:gcc g++ c++命令都为4.4.6(可用gcc -v; g++ -v; c++ -v 命令查 ...
随机推荐
- IDEA查看代码最近修改人及日期
如图,行号上右键,点击Annotate.即可查看
- Oracle连接Navicat Premium遇到的问题
ORA-28040: 没有匹配的验证协议. 通过查找资料找到了好的解决方案.可以不需要到官网上下载新的驱动来解决问题. 方法:在Oracle的安装路径下找到sqlnet.ora文件.(我的安 ...
- 【范式与函数依赖】3NF与BCNF的区别
*本文中码指代候选码,主属性为构成码的属性. 先简要引入几个概念 图1 图2 单拿出来我认为不是很好理解的3NF和BCNF详细的说说. 书上写了,BCNF是完善后的3NF.从图2中显然得出,1-3NF ...
- ZJNU 2133 - 认亲大会
将辈分差距转为数字 例如 A 是 B son A=B-1 A 是 B grandfather A=B+2然后编号1数字设置为0,建图bfs 最后搜索编号2到100是否存在>0的数即可 /* Wr ...
- ⼩程序中⽀持es7的async语法
⼩程序中⽀持es7的async语法 es7的 async 号称是解决回调的最终⽅案 在⼩程序的开发⼯具中,勾选 es6转es5语法 下载 facebook 的 regenerator 库中的 rege ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之字典类型(dict)
字典!在Python中字典是另一种可变容器模型,可以存储任意类型的对象.是Python存储数据类型的最高级(maybe). 特点:1. 字典的存储方式和其他类型数据略有不同,是通过键(key)和值(v ...
- Matlab高级教程_第一篇:Matlab基础知识提炼_01
第一篇:Matlab基础知识提炼: 这一篇主要用系统和提炼性的语言对Matlab基础知识进行总结,主要适用于有语言基础的学习者.尽量不讲废话. 第一部分:Matlab是什么? 1 Matlab是Mat ...
- MFC的cstring判断是否存在中文字符
bool IsChinese(CString Cstr) { int nLen = Cstr.GetLength(); unsigned char ch1, ch2; for (int i = 0; ...
- Qt的窗口如何显示在spi屏幕上
问题:我们可以将qt移植到H3上,但是如果要将窗口通过spi屏进行显示应该怎么做呢,和framebuffer又有什么关系? 这里只说一下qt在spi屏上显示的技术原理,具体的实现还需要自己努力. 一. ...
- zabbix数据库占用磁盘空间较大的处理方法
du -h /* |sort -nr 使用此命令一步步排查发现/var/lib/mysql/zabbix/这个目录占用磁盘空间较大 发现history_log.ibd这个文件最大,达到了38G,此文 ...