仿照写的sina微博的简单爬虫
1、创建工程
scrapy startproject sinablog
2、修改items.py
import scrapyclass SinablogItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()article_name = scrapy.Field()article_url = scrapy.Field()
3、修改pipelines.py
import json
import codecs class SinablogPipeline(object): def __init__(self): self.file = codecs.open('sinablog_data.json', mode='wb', encoding='utf-8') def process_item(self, item, spider): line = json.dumps(dict(item)) + '\n' self.file.write(line.decode("unicode_escape")) return item
4、修改settings.py
BOT_NAME = 'sinablog'SPIDER_MODULES = ['sinablog.spiders']
NEWSPIDER_MODULE = 'sinablog.spiders'#禁止cookies,防止被ban
COOKIES_ENABLED = FalseITEM_PIPELINES = {'sinablog.pipelines.SinablogPipeline':300
}# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'sinablog (+http://www.yourdomain.com)'
5、新建sinablog_spider.py
from scrapy.spider import Spider
from scrapy.http import Request
from scrapy.selector import Selector
from sinablog.items import SinablogItem class SinablogSpider(Spider): """爬虫SinablogSpider""" name = "sinablog" #减慢爬取速度 为1s download_delay = 1 allowed_domains = ["blog.sina.com.cn"] start_urls = [#第一篇文章地址 "http://blog.sina.com.cn/s/blog_65db99840100h2jj.html" ] def parse(self, response): sel = Selector(response) #items = [] #获得文章url和标题 item = SinablogItem() article_url = str(response.url) article_name = sel.xpath('//h2[@class="titName SG_txta"]/text()').extract() item['article_name'] = [n.encode('utf-8') for n in article_name] item['article_url'] = article_url.encode('utf-8') yield item #获得下一篇文章的url urls = sel.xpath('//div[@class="articalfrontback SG_j_linedot1 clearfix"]/div/a/@href').extract() for url in urls: #print url #url = "http://blog.csdn.net" + url print url yield Request(url, callback=self.parse)
6、运行爬虫
scrapy crawl sinablog
执行结果如下:
{"article_name": ["欢迎您在新浪博客安家"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100h2jj.html"}
{"article_name": ["WIN7系统有杂音"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100jpb8.html"}
{"article_name": ["近视手术,想好再做"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100jphn.html"}
{"article_name": ["欢迎您在新浪博客安家"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100h2jj.html"}
{"article_name": ["wxToolBar::Realize()"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100jwav.html"}
{"article_name": ["指针类型转换---小问题害死人"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100jyha.html"}
{"article_name": ["Windows下OverSim和PeerSim的安装"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100k287.html"}
{"article_name": ["C语言读取文件一行以及换行符的问题"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kguj.html"}
{"article_name": ["C语言写一行及写换行符的问题"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100khaf.html"}
{"article_name": ["文本模式读写文件中\r和\n的问题"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kidc.html"}
{"article_name": ["string和char*"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kie9.html"}
{"article_name": ["标准输入输出流是二进制流"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kiem.html"}
{"article_name": ["extern C"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kjr2.html"}
{"article_name": ["opera unite"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kjsd.html"}
{"article_name": ["error C2146: 语法错误 : 缺少“;”"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kkiy.html"}
{"article_name": ["ip地址什么时候要用16位char数组存放"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kkni.html"}
{"article_name": ["LNK2019 MeridianClient.def"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kl2n.html"}
{"article_name": ["无法定位xxx于动态链接库xxx.dll上"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kl5k.html"}
{"article_name": ["关山派出所办证大厅工作时间"], "article_url": "http://blog.sina.com.cn/s/blog_65db99840100kl9b.html"}
仿照写的sina微博的简单爬虫相关推荐
- 基于人人网的简单爬虫(一)——正则表达式
应课程实验要求,要写一个基于人人网的简单爬虫.实验要求如下: 学会使用一种编程语言实现爬取人人网关系网络的程序.该程序功能如下: 1. 能够输入用户登陆所产生的cookie,允许爬虫对人人网进行爬取 ...
- pythonurllib登录微博账号_简单爬虫实现登录新浪微博(python2.7)
因为图论作业,所以要写一个爬虫,就开始学python.接触python开始,就觉得这个语言非常舒服,不需要定义变量,不需要分号,非常简洁. 下面就聊聊,我写爬虫的经历.上网搜了一下爬虫的代码,发现简单 ...
- java用爬虫爬一个页面_使用Java写一个简单爬虫爬取单页面
使用Java爬虫爬取人民日报公众号页面图片 使用Java框架Jsoup和HttpClient实现,先看代码 爬取目标页面 1.使用Maven构建一个普通Java工程 加入依赖: org.jsoup j ...
- 如何使用php写爬虫,PHP如何开发简单爬虫
有时候因为工作.自身的需求,我们都会去浏览不同网站去获取我们需要的数据,于是爬虫应运而生,下面是开发一个简单爬虫的经过与遇到的问题.开发一个爬虫,首先你要知道你的这个爬虫是要用来做什么的.我是要用来去 ...
- python爬取微博内容_Python 爬虫如何机器登录新浪微博并抓取内容?
最近为了做事件分析写了一些微博的爬虫,两个大V总共爬了超70W的微博数据. 官方提供的api有爬取数量上限2000,想爬取的数据大了就不够用了... 果断撸起袖子自己动手!先简单说一下我的思路: 一. ...
- 使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图
第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到"煎蛋网xxoo"网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保 ...
- python爬虫源码附注解_Python小白写的三个入门级的爬虫(附代码和注释)
Python小白写的三个入门级的爬虫(附注释) 写在前面的话:作者目前正在学习Python,还是一名小白,所以注释可以会有些不准确的地方,望谅解. 这三个小爬虫不是很难,而且用处可能也不大,主要还是锻 ...
- .net core 实现简单爬虫—抓取博客园的博文列表
一.介绍一个Http请求框架HttpCode.Core HttpCode.Core 源自于HttpCode(传送门),不同的是 HttpCode.Core是基于.net standard 2.0实现的 ...
- python 百度百科 爬虫_python简单爬虫
爬虫真是一件有意思的事儿啊,之前写过爬虫,用的是urllib2.BeautifulSoup实现简单爬虫,scrapy也有实现过.最近想更好的学习爬虫,那么就尽可能的做记录吧.这篇博客就我今天的一个学习 ...
最新文章
- 在一个数组中查找两个重复出现的数字
- angular select设置默认选中_技术分享 | Charset 和 Collat??ion 设置对 MySQL 性能的影响...
- 手持gps坐标转换参数求解方法及在excel中的实现_怎么在GIS office 软件中批量导入坐标点位...
- 织梦ajax表单提交参数错误,【织梦二次开发】织梦jquery+ajax方式提交自定义表单...
- 2020计算机语言排行 rust,RedMonk 2020 年 Q3 编程语言排行:Rust 首次进入前 20
- (111)FPGA面试题-介绍Verilog 块语句fork-join执行过程
- 神经网络技巧篇之寻找最优参数的方法
- Python入门--第三方模块的安装与使用,pip,import
- 利用natapp做内网穿透
- TXT文本 本地词典
- FudanNLP 复旦分词系统 小试一把
- 电脑出现"该内存不能为read、written "的完全解决方案
- 4-20MA/0-5V/0-3V隔离模块有哪些重要的功能?
- vue前端页面素材代码大全
- 汽车自动变速器的共性技术
- python 中搞错工作路径的意思导致的相对路径产生bug:[Errno 2] No such file or directory:
- linux scp密码参数,linux-scp不输入密码 - 不断的前进ING。。。 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
- 13、Nepxion Discovery 之 全链路调用链监控
- IT 路,梦里思路长。
- 编写一个截取字符串的函数
热门文章
- CSS3绘画卡通形象
- 大数据最重要的算法是什么,最常用的算法有哪些?
- 元宵节就要到了,手把手教你用Python打造一款3D花灯
- 【游戏引擎开发必问】 渲染管线的剖析
- Oracle 11g安装报错You do not have sufficient permissions to access the inventory
- 哲学家就餐问题python解决_关于哲学家就餐问题的分析代码.
- L1-057~L1063 (PTA使我精神焕发、6翻了、敲笨钟、心理阴影面积、新胖子公式、幸运彩票、吃鱼还是吃肉)
- 使用 Fiddler 进行 iOS APP 的 HTTP/HTTPS 抓包
- SUST OJ 1641: 电子狗的心事
- 给我新人生的skycc营销软件