编写自动爬取网页的爬虫
来源:互联网 发布:淘宝卖家如何办理发票 编辑:程序博客网 时间:2024/05/22 00:43
一、item的编写
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass AutopjtItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field()#定义好name用来存储商品名 name=scrapy.Field()#定义好price用来存储商品价格 price=scrapy.Field()#定义好link用来存储商品链接 link=scrapy.Field()#定义好comnum用来存储商品评论数 comnum=scrapy.Field()
二、pipeline的编写
# -*- coding: utf-8 -*-import codecsimport json# 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.htmlclass AutopjtPipeline(object): def __init__(self): #打开mydata.json文件 self.file = codecs.open("F:/mydata3.json", "wb", encoding="utf-8") def process_item(self, item, spider): # item=dict(item) # print(len(item["name"])) # 每一页中包含多个商品信息,所以可以通过循环,每一次处理一个商品 # 其中len(item["name"])为当前页中商品的总数,依次遍历 for j in range(0, len(item["name"])): # 将当前页的第j个商品的名称赋值给变量name name = item["name"][j] price = item["price"][j] comnum = item["comnum"][0] link = item["link"][j] # 将当前页下第j个商品的name、price、comnum、link等信息处理一下 # 重新组合成一个字典 goods = {"name": name, "price": price, "comnum": comnum, "link": link} #goods = {"name": name, "price": price, "link": link} # 将组合后的当前页中第j个商品的数据写入json文件 i = json.dumps(dict(goods), ensure_ascii=False) line = i + '\n' self.file.write(line) # 返回item return item def close_spider(self,spider):#关闭mydata.json文件 self.file.close()三、setting的设置
#开启pipeline功能
ITEM_PIPELINES = { 'autopjt.pipelines.AutopjtPipeline': 300,}
#禁用cookie
COOKIES_ENABLED = False#不遵守机器人协议
ROBOTSTXT_OBEY = False四、爬虫的编写
(1)分析当当网页面
(2)爬虫编写
# -*- coding: utf-8 -*-import scrapyfrom autopjt.items import AutopjtItemfrom scrapy.http import Requestclass AutospdSpider(scrapy.Spider): name = 'autospd' allowed_domains = ['dangdang.com'] start_urls = ['http://category.dangdang.com/pg1-cid4002203.html'] def parse(self, response): pass def parse(self, response): item = AutopjtItem() # 通过各Xpath表达式分别提取商品的名称、价格、链接、评论数等信息 item["name"] = response.xpath("//a[@class='pic']/@title").extract() item["price"] = response.xpath("//span[@class='price_n']/text()").extract() item["link"] = response.xpath("//a[@class='pic']/@href").extract() item["comnum"] = response.xpath("//a[@name='itemlist-review']/text()").extract() # 提取完后返回item yield item # 接下来很关键,通过循环自动爬取75页的数据 for i in range(1, 76): # 通过上面总结的网址格式构造要爬取的网址 url = "http://category.dangdang.com/pg" + str(i) + "-cid4002203.html" # 通过yield返回Request,并指定要爬取的网址和回调函数 # 实现自动爬取 yield Request(url, callback=self.parse)五、爬取结果
阅读全文
0 0
- 编写自动爬取网页的爬虫
- Python编写网页爬虫爬取oj上的代码信息
- Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
- jsoup爬虫爬取网页
- 爬虫爬虫学习: 爬取网页图片
- Python 网络爬虫 004 (编程) 如何编写一个网络爬虫,来下载(或叫:爬取)一个站点里的所有网页
- java编写的爬虫爬取煎蛋的图片
- Scala语言编写的爬虫应用-爬取一部小说
- 一个简单的网络爬虫---爬取网页中的图片
- [python爬虫]如何爬取特定网页的图片
- 定向爬虫:动态加载网页的爬取
- 爬虫入门(四) ajax网页的爬取
- 爬虫-爬取代理ip网页里的ip
- 爬虫实战2—动态网页的爬取
- 爬虫实例:爬取一个网页上的图片地址
- Python爬虫---爬取静态网页的图片
- 简单的实现爬虫爬取网页文本和图片
- Python可自动登录爬取图片的网络爬虫
- Maximum Product
- Ubuntu16.04下安装最新版本的CMake
- echarts颜色渐变
- Spark 内存管理
- java基础—java获取图片的大小和尺寸(本地& 服务器)
- 编写自动爬取网页的爬虫
- iOS应用生命周期
- socket tcp客户端
- Google人工智能平台TensorFlow介绍
- MySQL干货--常用的语句
- redis持久化
- 【软件安全】cwe-78 OS Command Injection
- 2017 Multi-University Training Contest
- Spring Boot (六) JdbcTemplate