scrapy爬虫系列之二--翻页爬取及日志的基本用法
功能点:如何翻页爬取信息,如何发送请求,日志的简单实用
爬取网站:腾讯社会招聘网
完整代码:https://files.cnblogs.com/files/bookwed/tencent.zip
主要代码:
job.py
# -*- coding: utf-8 -*-
import scrapy
from tencent.items import TencentItem
import logging # 日志模块 logger = logging.getLogger(__name__) class JobSpider(scrapy.Spider):
"""职位爬虫"""
name = 'job'
allowed_domains = ["tencent.com"]
offset = 0
baseUrl = "https://hr.tencent.com/position.php?start={}"
start_urls = [baseUrl.format(offset)] def parse(self, response):
# //tr[@class="even" or @class="odd"]
# xpath(),返回一个含有selector对象的列表
job_list = response.xpath("//tr[@class='even'] | //tr[@class='odd']")
for job in job_list:
item = TencentItem()
# extract() 提取字符串,返回一个包含字符串数据的列表
# extract_first(),返回列表中的第一个字符串
# extract()[0] 可以替换成extract_first(),不用再进行判断是否为空了
item["name"] = job.xpath("./td[1]/a/text()").extract_first()
item["url"] = job.xpath("./td[1]/a/@href").extract()[0]
item["type"] = job.xpath("./td[2]/text()")
item["type"] = item["type"].extract()[0] if len(item["type"]) > 0 else None
item["people_number"] = job.xpath("./td[3]/text()").extract()[0]
item["place"] = job.xpath("./td[4]/text()").extract()[0]
item["publish_time"] = job.xpath("./td[5]/text()").extract()[0]
# 打印方式1
# logging.warning(item)
# 打印方式2,【推荐,可以看到是哪个文件打印的】
logger.warning(item)
# 为什么使用yield?好处?
# 让整个函数变成一个生成器。每次遍历的时候挨个读到内存中,不会导致内存的占用量瞬间变高
yield item # 第一种:拼接url
# if self.offset < 3090:
# self.offset += 10
# url = self.baseUrl.format(self.offset)
# yield scrapy.Request(url, callback=self.parse) # yield response.follow(next_page, self.parse) # 第二种:从response获取要爬取的链接,并发送请求处理,知道链接全部提取完
if len(response.xpath("//a[@class='noactive' and @id='next']")) == 0:
temp_url = response.xpath("//a[@id='next']/@href").extract()[0]
# yield response.follow("https://hr.tencent.com/"+temp_url, callback=self.parse)
yield scrapy.Request(
"https://hr.tencent.com/"+temp_url,
callback=self.parse,
# meta={"item": item} # meta实现在不同的解析函数中传递数据
# dont_filter=True # 重复请求
) # 此处的callback指返回的响应由谁进行解析,如果和第一页是相同的处理,则用parse,否则定义新方法,指定该新方法 def parse1(self, response):
item = response.meta["item"]
print(item)
print("*"*30)
pipelines.py
import json class TencentPipeline(object):
# 可选实现,参数初始化等
def __init__(self):
self.f = open('tencent_job.json', 'w', encoding='utf-8') def process_item(self, item, spider):
# item(Item对象) - 被爬取的item
# spider(Spider对象)- 爬取item时的spider;通过spider.name可以获取爬虫名称
content = json.dumps(dict(item), ensure_ascii=False)+",\n"
self.f.write(content)
return item def open_spider(self, spider):
# 可选,spider开启时,该方法被调用
pass def close_spider(self, spider):
# 可选,spider关闭时,该方法被调用
self.f.close()
scrapy爬虫系列之二--翻页爬取及日志的基本用法的更多相关文章
- 爬虫简单之二---使用进程爬取起点中文网的六万多也页小说的名字,作者,等一些基本信息,并存入csv中
爬虫简单之二---使用进程爬取起点中文网的六万多也页小说的名字,作者,等一些基本信息,并存入csv中 准备使用的环境和库Python3.6 + requests + bs4 + csv + multi ...
- 爬虫系列4:Requests+Xpath 爬取动态数据
爬虫系列4:Requests+Xpath 爬取动态数据 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参 ...
- Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章
二.伯乐在线爬取所有文章 1. 初始化文件目录 基础环境 python 3.6.5 JetBrains PyCharm 2018.1 mysql+navicat 为了便于日后的部署:我们开发使用了虚拟 ...
- 爬虫系列1:Requests+Xpath 爬取豆瓣电影TOP
爬虫1:Requests+Xpath 爬取豆瓣电影TOP [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]: ...
- scrapy爬虫笔记(三)------写入源文件的爬取
开始爬取网页:(2)写入源文件的爬取 为了使代码易于修改,更清晰高效的爬取网页,我们将代码写入源文件进行爬取. 主要分为以下几个步骤: 一.使用scrapy创建爬虫框架: 二.修改并编写源代码,确定我 ...
- 爬虫系列3:Requests+Xpath 爬取租房网站信息并保存本地
数据保存本地 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参考前文 爬虫系列2:https://www ...
- 爬虫系列2:Requests+Xpath 爬取租房网站信息
Requests+Xpath 爬取租房网站信息 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参考前文 ...
- 爬虫系列(1)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- Scrapy实战篇(二)之爬取链家网成交房源数据(下)
在上一小节中,我们已经提取到了房源的具体信息,这一节中,我们主要是对提取到的数据进行后续的处理,以及进行相关的设置. 数据处理 我们这里以把数据存储到mongo数据库为例.编写pipelines.py ...
随机推荐
- 使用libcurl源代码编译只是的问题
curl 7.21.6 + vs2005 就把curl的.c文件加到project中编译.报错信息非常古怪: setup_once.h(274) : error C2628: '<unnamed ...
- Prime is problem - 素数环问题
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
- Unity3D之碰撞体,刚体
一 概念介绍 刚体 Rigidbody(刚体)组件可使游戏对象在物理系统的控制下来运动,刚体可接受外力与扭矩力用来保证游戏对象像在真实世界中那样进行运动.任何游戏对象只有添加了刚体组件才能受到重力的影 ...
- 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现重复元素,当然也只允许有一个null对象. 2.如何来区分重复与否呢?(how) “ 用 iterator() ...
- PHP清除HTML代码、空格、回车换行符的函数
清除HTML代码.空格.回车换行符的函数如下 function DeleteHtml($str) { $str = trim($str); $str = strip_tags($str,"& ...
- localhost 和 127.0.0.1
转自:http://ordinarysky.cn/?p=431localhost与127.0.0.1的区别是什么?相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少 ...
- c++ list erase()
STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...
- ubuntu server install 安装中文(搜狗)输入法
1.对于ubuntu server默认无中文输入法框架,我比较倾向于我一直使用的ibus-sunpinyin.这里我需要先安装ibus的框架 不过我遇到了问题: dpkg: dependency pr ...
- nginx 杂谈
http://blog.sina.com.cn/s/articlelist_1834459124_0_1.html
- JavaScript------自定义string.replaceAll()方法
代码:: 注意:原始的replace()方法只能替换第一个字符串check String.prototype.replaceAll = function (s1, s2) { return this. ...