爬虫实例

  1. 预先设置好items
import scrapy
class SuperspiderItem(scrapy.Item):
title = scrapy.Field()
date = scrapy.Field()
content = scrapy.Field()
  1. 爬取范围和start_url
class Spider1Spider(scrapy.Spider):
name = 'spider1'
allowed_domains = ['http://wz.sun0769.com/']
start_urls = ['http://wz.sun0769.com/html/top/report.shtml']
  1. parse实现三大大功能抓取具体内容url链接和下一页url链接,并提取title和date
    def parse(self, response):
tr_list = response.xpath("//div[@class='newsHead clearfix']/table[2]//tr")
for tr in tr_list:
items = SuperspiderItem()
items['title'] = tr.xpath("./td[3]/a[1]/@title").extract_first() ##### 提取title 用xpath
items['date'] = tr.xpath("./td[6]//text()").extract_first() #### 同样的方法提取date
content_href = tr.xpath("./td[3]/a[1]/@href").extract_first() #### 提取内容链接
####---将提取的内容链接交给下一个函数,并将date和title也交给下一个函数最终数据统一处理---#########
####---有关yiled----####----content_url传url链接,callback指定回调函数----####
yield scrapy.Request(
content_href,
callback=self.get_content,
####----meta-可以将数据转移----####
####----一个类字典的数据类型----####
meta={
'date': items['date'],
'title': items['title']
}
)
new_url = response.xpath("//div[contains(@align,'center')]//@href").extract()
print(new_url[-2])
if "page="+str(page_num*30) not in new_url[-2]:
####---指明爬取的页数---####
yield scrapy.Request(
new_url[-2],
callback=self.parse
)
  1. 第二个函数

    -汇集所有的函数并 传给piplines
    def get_content(self, response):
items = SuperspiderItem()
items['date'] = response.meta['date']
items['title'] = response.meta['title']
items['content'] = response.xpath("//td[@class='txt16_3']/text()").extract_first()
yield items
  1. piplines里面并没做什么.因为没对数据进行什么处理,只是简单的将数据打印
class SuperspiderPipeline(object):
def process_item(self, item, spider):
items = item
print('*'*100)
print(items['date'])
print(items['title'])
print(items['content'])

完整代码

  • items里面的部分

import scrapy class SuperspiderItem(scrapy.Item):
title = scrapy.Field()
date = scrapy.Field()
content = scrapy.Field()
  • spider代码
# -*- coding: utf-8 -*-
import scrapy
from superspider.items import SuperspiderItem
page_num = 3
class Spider1Spider(scrapy.Spider):
name = 'spider1'
allowed_domains = ['wz.sun0769.com']
start_urls = ['http://wz.sun0769.com/html/top/report.shtml'] def parse(self, response):
tr_list = response.xpath("//div[@class='newsHead clearfix']/table[2]//tr")
for tr in tr_list:
items = SuperspiderItem()
items['title'] = tr.xpath("./td[3]/a[1]/@title").extract_first()
items['date'] = tr.xpath("./td[6]//text()").extract_first()
content_href = tr.xpath("./td[3]/a[1]/@href").extract_first()
yield scrapy.Request(
content_href,
callback=self.get_content,
meta={
'date': items['date'],
'title': items['title']
}
)
new_url = response.xpath("//div[contains(@align,'center')]//@href").extract()
print(new_url[-2])
if "page="+str(page_num*30) not in new_url[-2]:
yield scrapy.Request(
new_url[-2],
callback=self.parse
) def get_content(self, response):
items = SuperspiderItem()
items['date'] = response.meta['date']
items['title'] = response.meta['title']
items['content'] = response.xpath("//td[@class='txt16_3']/text()").extract_first()
yield items
  • piplines代码
class SuperspiderPipeline(object):
def process_item(self, item, spider):
items = item
print('*'*100)
print(items['date'])
print(items['title'])
print(items['content'])

中间遇到的问题

  • 爬取范围写错而日志等级又设置为warning,导致找不出问题
  • yield相关内容不清楚
  • 要先导入并初始化一个SuperspiderItem()(加括号)
  • piplines中不需要导入SuperspiderItem()
  • extract()忘写
  • xpath://div[contains(@align,'center')注意写法

最新文章

  1. 【WP 8.1开发】How to 图像处理
  2. Python for Infomatics 第12章 网络编程二(译)
  3. Unity3d热更新全书-加载(二)如何在不用AssetBundle的前提下动态加载预设
  4. iOS_UIImge_Gif的展示
  5. exports 和 module.exports 的区别
  6. struts 标签<s:ierator>的简单使用说明
  7. 免费好用的web应用托管平台
  8. 华为面试题——一道关于指针方面的编程题(C/C++)
  9. 如何采集所有QQ群成员?
  10. [Flex] ButtonBar系列——arrowKeysWrapFocus属性如果为 true,则使用箭头键在组件内导航时,如果击中某一端则将折回。
  11. unity HideInInspector 默认值 坑 记录 bug
  12. C++ char*,const char*,string,int 的相互转换
  13. CSU 1506(最小费用最大流)
  14. crontab定时任务
  15. 团队作业4---第一次项目冲刺(ALpha)版本 第七天
  16. 禁用了传说中的PHP危险函数之后,Laravel的定时任务不能执行了?
  17. 构建可扩展的微博架构(qcon beijing 2010演讲)#高并发经验值#
  18. 创建postgresql表空间
  19. th:标签
  20. Linux上磁盘热插拔

热门文章

  1. php基本数据类型解说
  2. IOS 手动添加第三方库报错问题
  3. iOS 缩小 ipa 大小
  4. B 方块消消乐
  5. 1272: 【基础】求P进制数的最大公因子与最小公倍数
  6. python:<class 'numpy.ndarray'>的学习
  7. js中使用Timer来计时程序执行时 - [javascript] - [开发]
  8. python 天天生鲜项目
  9. MySQL入门,第三部分,学会添加删除数据库
  10. 四、华为VRP平台介绍和常用配置