python3 scrapy 爬取腾讯招聘
安装scrapy不再赘述,
在控制台中输入scrapy startproject tencent 创建爬虫项目名字为 tencent
接着cd tencent
用pycharm打开tencent项目
构建item文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# -*- coding: utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass TencentItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() #职位名 positionname = scrapy.Field() #详细链接 positionLink = scrapy.Field() #职位类别 positionType = scrapy.Field() #招聘人数 peopleNum = scrapy.Field() #工作地点 workLocation = scrapy.Field() #发布时间 publishTime = scrapy.Field() |
接着在spiders文件夹中新建tencentPostition.py文件代码如下注释写的很清楚
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# -*- coding: utf-8 -*-import scrapyfrom tencent.items import TencentItemclass TencentpostitionSpider(scrapy.Spider): #爬虫名 name = 'tencent' #爬虫域 allowed_domains = ['tencent.com'] #设置URL url = 'http://hr.tencent.com/position.php?&start=' #设置页码 offset = 0 #默认url start_urls = [url+str(offset)] def parse(self, response): #xpath匹配规则 for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"): item = TencentItem() # 职位名 item["positionname"] = each.xpath("./td[1]/a/text()").extract()[0] # 详细链接 item["positionLink"] = each.xpath("./td[1]/a/@href").extract()[0] # 职位类别 try: item["positionType"] = each.xpath("./td[2]/text()").extract()[0] except: item["positionType"] = '空' # 招聘人数 item["peopleNum"] = each.xpath("./td[3]/text()").extract()[0] # 工作地点 item["workLocation"] = each.xpath("./td[4]/text()").extract()[0] # 发布时间 item["publishTime"] = each.xpath("./td[5]/text()").extract()[0] #把数据交给管道文件 yield item #设置新URL页码 if(self.offset<2620): self.offset += 10 #把请求交给控制器 yield scrapy.Request(self.url+str(self.offset),callback=self.parse) |
接着配置管道文件pipelines.py代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding: utf-8 -*-# 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.htmlimport jsonclass TencentPipeline(object): def __init__(self): #在初始化方法中打开文件 self.fileName = open("tencent.json","wb") def process_item(self, item, spider): #把数据转换为字典再转换成json text = json.dumps(dict(item),ensure_ascii=False)+"\n" #写到文件中编码设置为utf-8 self.fileName.write(text.encode("utf-8")) #返回item return item def close_spider(self,spider): #关闭时关闭文件 self.fileName.close() |
接下来需要配置settings.py文件
不遵循ROBOTS规则
|
1
|
ROBOTSTXT_OBEY = False |
|
1
2
|
#下载延迟DOWNLOAD_DELAY = 3 |
|
1
2
3
4
5
|
#设置请求头DEFAULT_REQUEST_HEADERS = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',} |
|
1
2
3
4
|
#交给哪个管道文件处理 文件夹.管道文件名.类名ITEM_PIPELINES = { 'tencent.pipelines.TencentPipeline': 300,} |
接下来再控制台中输入
scrapy crawl tencent
即可爬取
源码地址
https://github.com/ingxx/scrapy_to_tencent
python3 scrapy 爬取腾讯招聘的更多相关文章
- 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息
简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...
- 利用scrapy爬取腾讯的招聘信息
利用scrapy框架抓取腾讯的招聘信息,爬取地址为:https://hr.tencent.com/position.php 抓取字段包括:招聘岗位,人数,工作地点,发布时间,及具体的工作要求和工作任务 ...
- 『Scrapy』爬取腾讯招聘网站
分析爬取对象 初始网址, http://hr.tencent.com/position.php?@start=0&start=0#a (可选)由于含有多页数据,我们可以查看一下这些网址有什么相 ...
- scrapy 第一个案例(爬取腾讯招聘职位信息)
import scrapy import json class TzcSpider(scrapy.Spider): # spider的名字,唯一 name = 'tzc' # 起始地址 start_u ...
- python之scrapy爬取某集团招聘信息以及招聘详情
1.定义爬取的字段items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See do ...
- Python 爬取腾讯招聘职位详情 2019/12/4有效
我爬取的是Python相关职位,先po上代码,(PS:本人小白,这是跟着B站教学视频学习后,老师留的作业,因为腾讯招聘的网站变动比较大,老师的代码已经无法运行,所以po上),一些想法和过程在后面. f ...
- scrapy 爬取智联招聘
准备工作 1. scrapy startproject Jobs 2. cd Jobs 3. scrapy genspider ZhaopinSpider www.zhaopin.com 4. scr ...
- 利用Crawlspider爬取腾讯招聘数据(全站,深度)
需求: 使用crawlSpider(全站)进行数据爬取 - 首页: 岗位名称,岗位类别 - 详情页:岗位职责 - 持久化存储 代码: 爬虫文件: from scrapy.linkextractors ...
- python爬虫爬取腾讯招聘信息 (静态爬虫)
环境: windows7,python3.4 代码:(亲测可正常执行) import requests from bs4 import BeautifulSoup from math import c ...
随机推荐
- codevs & vijos 爱在心中 - Tarjan
描述 “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有 ...
- MFC、Qt、C#跨线程调用对象
MFC.Qt.C#都是面向对象的编程库 1.MFC不允许跨线程调用对象,即线程只能调用它本身分配了空间的对象 In a multi-threaded application written using ...
- Python3基础 issubclass 判断基类
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- windows下如何解决chrome浏览器左下角总提示'Downloading proxy script'的问题
答:关掉LAN settings中的Automatically detect settings选项和Use automatic configuration script选项,如下图
- 【查看内存参数详解】Linux free -m 详细说明
free 命令相对于top 提供了更简洁的查看系统内存使用情况:$ free total used ...
- MariaDB / MySQL数据类型
MariaDB / MySQL 数据类型 有三种主要的类型:Text(文本).Number(数字)和 Date/Time(日期/时间)类型. Text 类型: 数据类型 描述 CHAR(size) 保 ...
- jekins,报错 stderr: Could not create directory '/usr/share/tomcat7/.ssh'. Failed to add the host to the list of
public key是在~/.ssh/id_rsa.pub,而private key是~/.ssh/id_rsa 设置的时候,Jenkins需要的是private key
- linux 系统调用号表
位于 /usr/include/asm/unistd.h 由于我是64位系统,所以有一些额外的东西.我的这个文件为下文 #ifndef _ASM_X86_UNISTD_H #define _ASM_X ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- JAVA反射会降低你的程序性能吗?
原文出处 早两天写了<从把三千行代码重构成15行代码谈起>这篇文章,看到评论中有一些同学的回复还是在质疑反射的性能,好像程序用上了反射,就像开上了拖拉机似的.本来我觉得这个话题没有什么好讨 ...