All Projects → xiyouMc → V2EX_Spider

xiyouMc / V2EX_Spider

Licence: other
V2EX爬虫

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to V2EX Spider

Scrapy-Spiders
一个基于Scrapy的数据采集爬虫代码库
Stars: ✭ 34 (+61.9%)
Mutual labels:  spider, scrapy
devsearch
A web search engine built with Python which uses TF-IDF and PageRank to sort search results.
Stars: ✭ 52 (+147.62%)
Mutual labels:  spider, scrapy
Web-Iota
Iota is a web scraper which can find all of the images and links/suburls on a webpage
Stars: ✭ 60 (+185.71%)
Mutual labels:  spider, scrapy
Spider job
招聘网数据爬虫
Stars: ✭ 234 (+1014.29%)
Mutual labels:  spider, scrapy
photo-spider-scrapy
10 photo website spiders, 10 个国外图库的 scrapy 爬虫代码
Stars: ✭ 17 (-19.05%)
Mutual labels:  spider, scrapy
scrapy helper
Dynamic configurable crawl (动态可配置化爬虫)
Stars: ✭ 84 (+300%)
Mutual labels:  spider, scrapy
NScrapy
NScrapy is a .net core corss platform Distributed Spider Framework which provide an easy way to write your own Spider
Stars: ✭ 88 (+319.05%)
Mutual labels:  spider, scrapy
Goribot
[Crawler/Scraper for Golang]🕷A lightweight distributed friendly Golang crawler framework.一个轻量的分布式友好的 Golang 爬虫框架。
Stars: ✭ 190 (+804.76%)
Mutual labels:  spider, scrapy
Scrapy IPProxyPool
免费 IP 代理池。Scrapy 爬虫框架插件
Stars: ✭ 100 (+376.19%)
Mutual labels:  spider, scrapy
elves
🎊 Design and implement of lightweight crawler framework.
Stars: ✭ 322 (+1433.33%)
Mutual labels:  spider, scrapy
Spiderkeeper
admin ui for scrapy/open source scrapinghub
Stars: ✭ 2,562 (+12100%)
Mutual labels:  spider, scrapy
scrapy-distributed
A series of distributed components for Scrapy. Including RabbitMQ-based components, Kafka-based components, and RedisBloom-based components for Scrapy.
Stars: ✭ 38 (+80.95%)
Mutual labels:  spider, scrapy
Gerapy
Distributed Crawler Management Framework Based on Scrapy, Scrapyd, Django and Vue.js
Stars: ✭ 2,601 (+12285.71%)
Mutual labels:  spider, scrapy
python-fxxk-spider
收集各种免费的 Python 爬虫项目
Stars: ✭ 184 (+776.19%)
Mutual labels:  spider, scrapy
Py Elasticsearch Django
基于python语言开发的千万级别搜索引擎
Stars: ✭ 207 (+885.71%)
Mutual labels:  spider, scrapy
small-spider-project
日常爬虫
Stars: ✭ 14 (-33.33%)
Mutual labels:  spider, scrapy
Marmot
💐Marmot | Web Crawler/HTTP protocol Download Package 🐭
Stars: ✭ 186 (+785.71%)
Mutual labels:  spider, scrapy
Scrapydweb
Web app for Scrapyd cluster management, Scrapy log analysis & visualization, Auto packaging, Timer tasks, Monitor & Alert, and Mobile UI. DEMO 👉
Stars: ✭ 2,385 (+11257.14%)
Mutual labels:  spider, scrapy
163Music
163music spider by scrapy.
Stars: ✭ 60 (+185.71%)
Mutual labels:  spider, scrapy
OpenScraper
An open source webapp for scraping: towards a public service for webscraping
Stars: ✭ 80 (+280.95%)
Mutual labels:  spider, scrapy

来自公众号 : DeveloperPython

开头

这两天后台收到了很多读者发消息说:“看了之前写的关于爬虫的文章之后,自己也想写一个爬虫但不知从何下手”。那么我今天就分享一个简单的案例,和大家一起从零写一个简单的爬虫。

在开始分享之前,我想提一件事情。

我知道,爬虫其实在部分外行人心目中一直是一个低劣或者低俗的人才做的事。那么,不管你是不是这么想,我只能说一句:要是没有爬虫我相信很多公司根本就没法起来。

那么,今天我主要通过一个爬虫框架 Scrapy 来一步步实现爬取 V2EX 首页所有的热门文章,旨在让你掌握这个框架来爬取对自己有用的数据。

正文

** 一、Scrapy 是什么?**

官网: http://scrapy-chs.readthedocs.io/zh_CN/latest/intro/overview.html

Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。可以应用到数据挖掘,信息处理或存储历史数据等一系列的程序中。

其最初是为了页面爬取(更确切的来说,网络爬取)所设计的,也可以应用在获取API所返回的数据或者通用的网络爬虫。

Scrapy是一个非常强大且好用的爬虫框架,它不仅提供了一些开箱即用的基础组件,还提供了强大的自定义功能。

框架的学习就是修改配置文件,填充代码就可以了。

二、安装 Scrapy?

由于我是用 Mac 来开发的,所以安装命令也是 Mac 下的,至于 Window 和 Linux 可以参考安装。

pip install scrapy

当然,一开始你得有 Python 的开发环境,这里就不安利 Python 的安装方法了。直接百度即可。

三、用 Scrapy 创建一个项目

Scrapy 中提供了 startproject 命令来创建爬虫项目。命令如下:

scrapy startproject V2EX

我们创建一个项目 V2EX 用来爬取 V2 首页文章的所有信息。 其中:

spiders 文件夹下就是你要实现爬虫功能的核心代码。在 spiders 文件夹下创建一个 spider ,用来爬取 V2 首页文章。

scrapy.cfg 是项目的配置文件。

settings.py用于设置请求的参数,使用代理,爬虫数据后文件保存等等的。

四、Scrapy 爬取 V2 首页文章

1、新建 v2exSpider

在 spiders 文件夹下新建一个文件, v2exSpider.py

如上图,start_urls 中添加 v2ex 的首页地址,同时重写 parse 方法。这样 Spider 将基于 start_urls 中的地址进行访问,并将数据回调给 parse 方法。

其中,response 就是返回的网页数据。

处理好的数据放在 items 中,在 items.py 设置好要处理哪些数据字段。这里我们来抓取 V2 首页的:作者地址、作者头像、文章地址、所属节点、作者昵称、最后一次回复者昵称、最后一次回复者地址、最后一次回复时间。

那么,要解析处理哪些数据在 items.py 中定义好,也就相当于 java 中的实体类:

2、 分析 V2EX 首页各元素的 xpath

xpath 的概念可以在 60行代码拿到10G国外xx视频... 中了解,当然你可以直接看这个教程:

http://www.w3school.com.cn/xpath/index.asp

通过 Chrome 打开 v2ex.com ,同时在当前页面空白处点击右键,选中 inspect ,这样就可以看到当前页面的 Elements 。 (图片略大,耐心访问)

在这里我们可以分析出来每一篇文章的标题、地址等等的 xpath 路径。

同时,发现首页的50篇文章都是属于 div[@class='cell item'] 的数据,因此我们可以通过  selector.xpath('//div[@class="cell item"]')  拿到所有文章的数据,然后再分析出具体数据的 xpath ,从而拿到了所有需要的数据。

解析的数据保存:

这时数据分析处理好了,还有最重要的一步,提交:

yield v2Item

OK! 万事俱备,数据保存在哪里,什么格式?

在 settings.py 中加入两行代码:

如何运行这个爬虫? scrapy crawl v2exSpider

这样就可以把 V2EX 的首页文章信息都爬取到了本地的 csv 文件中了。

最后,你会发现当前代码只能爬取 V2 中首页的文章,这时候你就需要分析到 v2ex 中下一页的 xpath ,然后拿到这个 url ,通过 yield Request(next_link,callback=self.parse),这样就可以一直爬取到 v2 最后一页的数据。 来看看数据:

总结

爬虫需谨慎,爬虫需有度。 本篇文章中项目的源代码托管在 Github,点击 【阅读原文】 。 ....end...

行为艺术要持之以恒,iOS专用赞赏通道。

长摁‘识别二维码’,一起进步

生活不止眼前的苟且,还有手下的代码、 和嘴上的扯淡 —— 个人博客: http://xiyoumc.0x2048.com Github:https://www.github.com/xiyouMc

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].