scrapy webmagic
来源:互联网 发布:做淘宝开店代理怎么样 编辑:程序博客网 时间:2024/05/04 14:54
最近要试图去爬新浪微博上的部分数据(还不知道新浪微博有没有封爬虫)。
1.开始使用了webmagic,官网上的源代码是maven开发的,我电脑maven没有下载库,搞了一天出了各种问题,就罢了。
2.然后改用scrapy,现在还在尝试,试了一个爬豆瓣的代码,发现豆瓣好像也ban了爬虫,不过不确定,先找个其他网站试试:米胖。
先讲一下scrapy使用,安装好了,创建框架:scrapy startproject douban
然后初始化爬虫scrapy genspider spider_name start_url
item,spider实现,download_delay设置。
爬取:
scrapy crawl
spider_name
3.今天一直碰到一个问题就是scrapy中spider调用上一级文件中的一个方法后,在spider中提示仍然没有定义。
开始官网并没有提这个问题,文件没有import,可以,但是我实现就不行了。
然后实现方法改为:from spider.spider.spiders.items import dmoz_spider,结果还是不行。
再次,试了import sys sys.path.append("..") import 上一级目录的那个文件,还是不行。
在其他位置做了类似实验,还是不行。
搞得回头丧气的,最后看到一个文章(https://segmentfault.com/q/1010000002402183)提到说不应该from spider.spider.spiders.items import dmoz_spider,需要from spider.items import dmoz_spider,因为在setting已经设定了module。。。试了试,刚才的item未定义已经没了,现在看看调试一下效果。(卧糙,官网上有这么句话,我杂就没看到哪。。。),测试可行。代码如下:
items.py
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyfrom scrapy.item import Fieldclass SpiderItem(scrapy.Item): # define the fields for your item here like: provincename = scrapy.Field()
spider.py
# -*- coding: utf-8 -*-import sysreload(sys)sys.setdefaultencoding('utf-8')import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders import Rulefrom spider.items import SpiderItemclass MipangSpider(scrapy.Spider): name = "mipang" allowed_domains = ["maps.mipang.com"] start_urls = ["http://maps.mipang.com/", ] rules = ( Rule(LinkExtractor(allow=r'start=\d{1,3}$'), callback='parse_item', follow=True), ) def parse(self, response): items = [] province_list = response.css('div.box.modbox.mod-weather-list') filename = "province" for province in province_list: item = SpiderItem() item['provincename'] = province.xpath('h4/a/text()').extract()[0] print "################################################" print filename with open(filename, 'wb') as f: print "################################################" print type(province.xpath('h4/a/text()').extract()[0]) f.write(province.xpath('h4/a/text()').extract()[0])
- scrapy webmagic
- webmagic
- WebMagic
- webmagic
- WebMagic-介绍
- webmagic爬虫
- webmagic入门
- WebMagic架构
- WebMagic Doc
- webmagic爬虫
- 初学webMagic
- webmagic实例
- webmagic学习
- WebMagic简述
- webmagic总结
- 初识WebMagic
- scrapy
- Scrapy
- Android Studio Template 模板初识及使用
- PHP安全编程之PHP的安全模式
- Bitcode适配指南
- Python 之 Scrapy笔记(1)- 入门篇
- 源码编译安装nginx
- scrapy webmagic
- 如何去掉Android Studio的注释警告
- CSS——NO.10(设置技巧)
- c#获取当前日期时间
- MySQL的简单的优化方式
- POJ 2247 Humble Numbers
- Linux查看系统各类信息
- Postman正确区分参数的几种类型
- 数据库问题集合