python3爬虫实例-python3 网络爬虫 实例1
scrapy
pip install scrapy
pip install pyOpenSSL
pip install cryptography
pip install CFFI
pip install lxml
pip install cssselect
pip install Twisted
创建爬虫项目
scrapy startproject zhipinSpider
生成爬虫
scrapy genspider job_position "zhipin.com"
image.png
目录结构:
items.py :
pipelines.py:处理爬取的内容
settings.py :配置文件
先调试数据
让scrapy伪装成浏览器
XPath语法
/ 匹配根节点
// 任意节点
. 当前节点
.. 父节点
@ 属性
//div[@title="xxx"]/div
extract提取节点内容
image.png
CSS匹配
image.png
items.py
import scrapy
class ZhipinspiderItem(scrapy.Item):
# 工作名称
title = scrapy.Field()
# 工资
salary = scrapy.Field()
# 招聘公司
company = scrapy.Field()
# 工作详细链接
url = scrapy.Field()
# 工作地点
work_addr = scrapy.Field()
# 行业
industry = scrapy.Field()
# 公司规模
company_size = scrapy.Field()
# 招聘人
recruiter = scrapy.Field()
# 发布时间
publish_date = scrapy.Field()
job_spider.py
import scrapy
from ZhipinSpider.items import ZhipinspiderItem
class JobPositionSpider(scrapy.Spider):
# 定义该Spider的名字
name = 'job_position'
# 定义该Spider允许爬取的域名
allowed_domains = ['zhipin.com']
# 定义该Spider爬取的首页列表
start_urls = ['https://www.zhipin.com/c101280100/h_101280100/']# 该方法负责提取response所包含的信息
# response代表下载器从start_urls中每个URL下载得到的响应
def parse(self, response):
# 遍历页面上所有//div[@class="job-primary"]节点
for job_primary in response.xpath('//div[@class="job-primary"]'):
item = ZhipinspiderItem()
# 匹配//div[@class="job-primary"]节点下/div[@class="info-primary"]节点
# 也就是匹配到包含工作信息的
元素
info_primary = job_primary.xpath('./div[@class="info-primary"]')
item['title'] = info_primary.xpath('./h3/a/div[@class="job-title"]/text()').extract_first()
item['salary'] = info_primary.xpath('./h3/a/span[@class="red"]/text()').extract_first()
item['work_addr'] = info_primary.xpath('./p/text()').extract_first()
item['url'] = info_primary.xpath('./h3/a/@href').extract_first()
# 匹配//div[@class="job-primary"]节点下./div[@class="info-company"]节点下
# 的/div[@class="company-text"]的节点
# 也就是匹配到包含公司信息的
元素
company_text = job_primary.xpath('./div[@class="info-company"]' +
'/div[@class="company-text"]')
item['company'] = company_text.xpath('./h3/a/text()').extract_first()
company_info = company_text.xpath('./p/text()').extract()
if company_info and len(company_info) > 0:
item['industry'] = company_info[0]
if company_info and len(company_info) > 2:
item['company_size'] = company_info[2]
# 匹配//div[@class="job-primary"]节点下./div[@class="info-publis"]节点下
# 也就是匹配到包含发布人信息的
元素
info_publis = job_primary.xpath('./div[@class="info-publis"]')
item['recruiter'] = info_publis.xpath('./h3/text()').extract_first()
item['publish_date'] = info_publis.xpath('./p/text()').extract_first()
yield item
# 解析下一页的链接
new_links = response.xpath('//div[@class="page"]/a[@class="next"]/@href').extract()
if new_links and len(new_links) > 0:
# 获取下一页的链接
new_link = new_links[0]
# 再次发送请求获取下一页数据
yield scrapy.Request("https://www.zhipin.com" + new_link, callback=self.parse)
pipelines.py
class ZhipinspiderPipeline(object):
def process_item(self, item, spider):
print("工作:" , item['title'])
print("工资:" , item['salary'])
print("工作地点:" , item['work_addr'])
print("详情链接:" , item['url'])print("公司:" , item['company'])
print("行业:" , item['industry'])
print("公司规模:" , item['company_size'])
print("招聘人:" , item['recruiter'])
print("发布日期:" , item['publish_date'])
settings.py
-- coding: utf-8 --
Scrapy settings for ZhipinSpider project
For simplicity, this file contains only settings considered important or
commonly used. You can find more settings consulting the documentation:
BOT_NAME = 'ZhipinSpider'
SPIDER_MODULES = ['ZhipinSpider.spiders']
NEWSPIDER_MODULE = 'ZhipinSpider.spiders'
Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'ZhipinSpider (+http://www.yourdomain.com)'
Obey robots.txt rules
ROBOTSTXT_OBEY = True
Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 32
Configure a delay for requests for the same website (default: 0)
See also autothrottle settings and docs
DOWNLOAD_DELAY = 3
The download delay setting will honor only one of:
CONCURRENT_REQUESTS_PER_DOMAIN = 16
CONCURRENT_REQUESTS_PER_IP = 16
Disable cookies (enabled by default)
COOKIES_ENABLED = False
Disable Telnet Console (enabled by default)
TELNETCONSOLE_ENABLED = False
Override the default request headers:
配置默认的请求头
DEFAULT_REQUEST_HEADERS = {
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'
}
Enable or disable spider middlewares
SPIDER_MIDDLEWARES = {'ZhipinSpider.middlewares.ZhipinspiderSpiderMiddleware': 543,}
Enable or disable downloader middlewares
DOWNLOADER_MIDDLEWARES = {'ZhipinSpider.middlewares.ZhipinspiderDownloaderMiddleware': 543,}
Enable or disable extensions
EXTENSIONS = {'scrapy.extensions.telnet.TelnetConsole': None,}
Configure item pipelines
配置使用Pipeline
ITEM_PIPELINES = { 'ZhipinSpider.pipelines.ZhipinspiderPipeline': 300, }
Enable and configure the AutoThrottle extension (disabled by default)
AUTOTHROTTLE_ENABLED = True
The initial download delay
AUTOTHROTTLE_START_DELAY = 5
The maximum download delay to be set in case of high latencies
AUTOTHROTTLE_MAX_DELAY = 60
The average number of requests Scrapy should be sending in parallel to
each remote server
AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
Enable showing throttling stats for every response received:
AUTOTHROTTLE_DEBUG = False
Enable and configure HTTP caching (disabled by default)
HTTPCACHE_ENABLED = True
HTTPCACHE_EXPIRATION_SECS = 0
HTTPCACHE_DIR = 'httpcache'
HTTPCACHE_IGNORE_HTTP_CODES = []
HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
启动
scrapy crawl job_position
存入数据库:pipelines.py
导入访问MySQL的模块
import mysql.connector
class ZhipinspiderPipeline(object):
# 定义构造器,初始化要写入的文件
def init(self):
self.conn = mysql.connector.connect(user='root', password='32147',
host='localhost', port='3306',
database='python', use_unicode=True)
self.cur = self.conn.cursor()
# 重写close_spider回调方法,用于关闭数据库资源
def close_spider(self, spider):
print('----------关闭数据库资源-----------')
# 关闭游标
self.cur.close()
# 关闭连接
self.conn.close()
def process_item(self, item, spider):
self.cur.execute("INSERT INTO job_inf VALUES(null, %s, %s, %s, %s, %s,
%s, %s, %s, %s)", (item['title'], item['salary'], item['company'],
item['url'], item['work_addr'], item['industry'],
item.get('company_size'), item['recruiter'], item['publish_date']))
self.conn.commit()
处理反爬虫
更改IP地址:middlewares.py
image.png
禁用cookie:settings.py
COOKIES_ENABLED=False
不遵守爬虫规则
image.png
设置访问频率
image.png
image.png
登录selenium
python3爬虫实例-python3 网络爬虫 实例1相关推荐
- python3.6网络爬虫_python3.6网络爬虫
<精通Python网络爬虫:核心技术.框架与项目实战>--导读 前 言 为什么写这本书 网络爬虫其实很早就出现了,最开始网络爬虫主要应用在各种搜索引擎中.在搜索引擎中,主要使用通用网络爬虫 ...
- 精通python网络爬虫-精通Python网络爬虫:核心技术、框架与项目实战 PDF
给大家带来的一篇关于Python爬虫相关的电子书资源,介绍了关于Python.Python网络爬虫.Python核心技术.Python框架.Python项目实战方面的内容,本书是由机械工业出版社出版, ...
- python爬虫设计模式_Python3网络爬虫(一):利用urllib进行简单的网页抓取
点击蓝色字免费订阅,每天收到这样的好信息 前言:最近有不少粉丝关注本公众号.并且我已经成功开通了流量主同时会赚一点点广告费,我打算每个月把这部分钱拿出来给大家买点书刊,算是给大家一点福利吧.大家想买什 ...
- 精通python网络爬虫-精通Python网络爬虫:核心技术、框架与项目实战
-- 目录 -- 前言 第一篇 理论基础篇 第1章 什么是网络爬虫 1.1 初识网络爬虫 1.2 为什么要学网络爬虫 1.3 网络爬虫的组成 1.4 网络爬虫的类型 1.5 爬虫扩展--聚焦爬虫 1. ...
- python商业爬虫教程_廖雪峰老师的Python商业爬虫课程 Python网络爬虫实战教程 体会不一样的Python爬虫课程...
廖雪峰老师的Python商业爬虫课程 Python网络爬虫实战教程 体会不一样的Python爬虫课程 1.JPG (53.51 KB, 下载次数: 1) 2019-8-9 08:15 上传 2.JPG ...
- python爬虫程序-Python网络爬虫实战(一)快速入门
本系列从零开始阐述如何编写Python网络爬虫,以及网络爬虫中容易遇到的问题,比如具有反爬,加密的网站,还有爬虫拿不到数据,以及登录验证等问题,会伴随大量网站的爬虫实战来进行. 我们编写网络爬虫最主要 ...
- Python 网络爬虫 001 (科普) 网络爬虫简介
Python 网络爬虫 001 (科普) 网络爬虫简介 1. 网络爬虫是干什么的 我举几个生活中的例子: 例子一: 我平时会将 学到的知识 和 积累的经验 写成博客发送到CSDN博客网站上,那么对于我 ...
- 爬虫分类——通用网络爬虫、聚焦网络爬虫、增量式网络爬虫、深层网络爬虫
爬虫分类 网络爬虫按照系统结构和实现技术,大致可以分为以下几种类型:通用网络爬虫.聚焦网络爬虫.增量式网络爬虫.深层网络爬虫. 实际的网络爬虫系统通常是几种爬虫技术相结合实现的 通用网络爬虫 通用网络 ...
- python爬虫什么意思-网络爬虫是什么(python爬虫有什么用)
在这个谈论数据的时代,数据是一件极其重要的事情.我们如何获取完整而全面的数据?这不是一项容易的任务. 如果你想做好大数据分析,光靠自己的努力或外围数据是远远不够的,你需要依靠"神秘的外力&q ...
- python 爬虫论_Python网络爬虫(理论篇)
欢迎关注公众号:Python爬虫数据分析挖掘,回复[开源源码]免费获取更多开源项目源码 网络爬虫的组成 网络爬虫由控制节点,爬虫节点,资源库构成. 网络爬虫的控制节点和爬虫节点的结构关系 控制节点(爬 ...
最新文章
- 中国700万程序员不够用怎么办?我们去问了北大谢涛,顶会ASE最有影响力论文奖首批华人得主...
- 基于python的游戏设计与实现-python五子棋游戏的设计与实现
- Design Patterns in Java
- django虚拟环境搭建
- 如何使用openssl生成RSA公钥和私钥对
- python describe include_Python describe包_程序模块 - PyPI - Python中文网
- Teechart画图,MFC画图
- Proteus安装图文教程
- avast:中兴手机预装恶意软件 嵌入固件底层
- 复旦计算机系统基础课件,复旦大学软件工程考研(MSE)计算机系统基础复习资料.ppt...
- 【专题】拉格朗日中值定理求极限
- 什么是客户旅程分析(customer journey mapping)
- STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
- h5调用指纹识别_Vue指纹识别验证 h5plus
- 计算机专业调剂化学专业,汕头大学计算机化学专业2015年考研调剂信息
- “视”不可挡:征兵招警,近视手术成“通关法宝”
- java 基础知识(不定期更新)
- try 、catch、finally用法总结
- 【组图】世界著名城市夜景
- 数字电视音视频马赛克和不同步现象原因
热门文章
- java--内存管理的几点小技巧
- solaris UFS文件系统 要点
- java之IO整理(中)
- 20150917html第二次课
- 《javascript设计模式》笔记之第七章:工厂模式
- Ng-template寄宿方式
- DevExpress GridControl使用(二)
- Column 'Column Name' does not belong to table Table
- python编程入门电子书下载-最经典的25本Python编程开发电子书(附下载地址)!...
- 北京理工大学python系列课程-北理工《Python语言程序设计》荣获中国最美慕课一等奖...