首页 技术 正文
技术 2022年11月10日
0 收藏 725 点赞 2,467 浏览 9269 个字

items.py

 # -*- coding: utf-8 -*- # Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy
class LagouItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
#id
obj_id=scrapy.Field()
#职位名
positon_name=scrapy.Field()
#工作地点
work_place=scrapy.Field()
#发布日期
publish_time=scrapy.Field()
#工资
salary=scrapy.Field()
#工作经验
work_experience=scrapy.Field()
#学历
education=scrapy.Field()
#full_time
full_time=scrapy.Field()
#标签
tags=scrapy.Field()
#公司名字
company_name=scrapy.Field()
# #产业
# industry=scrapy.Field()
#职位诱惑
job_temptation=scrapy.Field()
#工作描述
job_desc=scrapy.Field()
#公司logo地址
logo_image=scrapy.Field()
#领域
field=scrapy.Field()
#发展阶段
stage=scrapy.Field()
#公司规模
company_size=scrapy.Field()
# 公司主页
home = scrapy.Field()
#职位发布者
job_publisher=scrapy.Field()
#投资机构
financeOrg=scrapy.Field()
#爬取时间
crawl_time=scrapy.Field()

spiders>lagou.py

 # -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from LaGou.items import LagouItem
from LaGou.utils.MD5 import get_md5
from datetime import datetime class LagouSpider(CrawlSpider):
name = 'lagou'
allowed_domains = ['lagou.com']
start_urls = ['https://www.lagou.com/zhaopin/']
content_links=LinkExtractor(allow=(r"https://www.lagou.com/jobs/\d+.html"))
page_links=LinkExtractor(allow=(r"https://www.lagou.com/zhaopin/\d+"))
rules = (
Rule(content_links, callback="parse_item", follow=False),
Rule(page_links,follow=True)
) def parse_item(self, response):
item=LagouItem()
#获取到公司拉钩主页的url作为ID
item["obj_id"]=get_md5(response.url)
#公司名称
item["company_name"]=response.xpath('//dl[@class="job_company"]//a/img/@alt').extract()[0]
# 职位
item["positon_name"]=response.xpath('//div[@class="job-name"]//span[@class="name"]/text()').extract()[0]
#工资
item["salary"]=response.xpath('//dd[@class="job_request"]//span[1]/text()').extract()[0]
# 工作地点
work_place=response.xpath('//dd[@class="job_request"]//span[2]/text()').extract()[0]
item["work_place"]=work_place.replace("/","")
# 工作经验
work_experience=response.xpath('//dd[@class="job_request"]//span[3]/text()').extract()[0]
item["work_experience"]=work_experience.replace("/","")
# 学历
education=response.xpath('//dd[@class="job_request"]//span[4]/text()').extract()[0]
item["education"]=education.replace("/","")
# full_time
item['full_time']=response.xpath('//dd[@class="job_request"]//span[5]/text()').extract()[0]
#tags
tags=response.xpath('//dd[@class="job_request"]//li[@class="labels"]/text()').extract()
item["tags"]=",".join(tags)
#publish_time
item["publish_time"]=response.xpath('//dd[@class="job_request"]//p[@class="publish_time"]/text()').extract()[0]
# 职位诱惑
job_temptation=response.xpath('//dd[@class="job-advantage"]/p/text()').extract()
item["job_temptation"]=",".join(job_temptation)
# 工作描述
job_desc=response.xpath('//dd[@class="job_bt"]/div//p/text()').extract()
item["job_desc"]=",".join(job_desc).replace("\xa0","").strip()
#job_publisher
item["job_publisher"]=response.xpath('//div[@class="publisher_name"]//span[@class="name"]/text()').extract()[0]
# 公司logo地址
logo_image=response.xpath('//dl[@class="job_company"]//a/img/@src').extract()[0]
item["logo_image"]=logo_image.replace("//","")
# 领域
field=response.xpath('//ul[@class="c_feature"]//li[1]/text()').extract()
item["field"]="".join(field).strip()
# 发展阶段
stage=response.xpath('//ul[@class="c_feature"]//li[2]/text()').extract()
item["stage"]="".join(stage).strip()
# 投资机构
financeOrg=response.xpath('//ul[@class="c_feature"]//li[3]/p/text()').extract()
if financeOrg:
item["financeOrg"]="".join(financeOrg)
else:
item["financeOrg"]=""
#公司规模
if financeOrg:
company_size= response.xpath('//ul[@class="c_feature"]//li[4]/text()').extract()
item["company_size"]="".join(company_size).strip()
else:
company_size = response.xpath('//ul[@class="c_feature"]//li[3]/text()').extract()
item["company_size"] = "".join(company_size).strip()
# 公司主页
item["home"]=response.xpath('//ul[@class="c_feature"]//li/a/@href').extract()[0]
# 爬取时间
item["crawl_time"]=datetime.now() yield item

pipelines.py

 # -*- 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.html import pymysql
class LagouPipeline(object): def process_item(self, item, spider):
con = pymysql.connect(host="127.0.0.1", user="root", passwd="", db="lagou",charset="utf8")
cur = con.cursor()
sql = ("insert into lagouwang(obj_id,company_name,positon_name,salary,work_place,work_experience,education,full_time,tags,publish_time,job_temptation,job_desc,job_publisher,logo_image,field,stage,financeOrg,company_size,home,crawl_time)"
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)")
lis=(item["obj_id"],item["company_name"],item["positon_name"],item["salary"],item["work_place"],item["work_experience"],item["education"],item['full_time'],item["tags"],item["publish_time"],item["job_temptation"],item["job_desc"],item["job_publisher"],item["logo_image"],item["field"],item["stage"],item["financeOrg"],item["company_size"],item["home"],item["crawl_time"])
cur.execute(sql, lis)
con.commit()
cur.close()
con.close() return item

middlewares.py

 from scrapy import signals
import random
#from LaGou.settings import USER_AGENTS
from fake_useragent import UserAgent class RandomUserAgent(object):
# def __init__(self,crawl):
# super(RandomUserAgent,self).__init__()
# self.ua=UserAgent()
def process_request(self, request, spider):
#useragent = random.choice(USER_AGENTS)
ua=UserAgent()
request.headers.setdefault("User-Agent",ua.random)

settings.py

 # -*- coding: utf-8 -*- # Scrapy settings for LaGou project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# http://doc.scrapy.org/en/latest/topics/settings.html
# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html BOT_NAME = 'LaGou' SPIDER_MODULES = ['LaGou.spiders']
NEWSPIDER_MODULE = 'LaGou.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'LaGou (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = False # Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32 # Configure a delay for requests for the same website (default: 0)
# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 5
# 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
# USER_AGENTS = [
# "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
# "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
# "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
# "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
# "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
# "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
# "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
# "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5"
# ]
# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False # Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#}
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
SCHEDULER_PERSIST = True
# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'LaGou.middlewares.LagouSpiderMiddleware': 543, #} # Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
'LaGou.middlewares.RandomUserAgent': 1,
# 'LaGou.middlewares.MyCustomDownloaderMiddleware': 543,
} # Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#} # Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'scrapy_redis.pipelines.RedisPipeline':300, #'LaGou.pipelines.LagouPipeline': 300,
} # Enable and configure the AutoThrottle extension (disabled by default)
# See http://doc.scrapy.org/en/latest/topics/autothrottle.html
#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)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

redis数据:

scrapy实战2分布式爬取lagou招聘(加入了免费的User-Agent随机动态获取库 fake-useragent 使用方法查看:https://github.com/hellysmile/fake-useragent)

mysql数据:

scrapy实战2分布式爬取lagou招聘(加入了免费的User-Agent随机动态获取库 fake-useragent 使用方法查看:https://github.com/hellysmile/fake-useragent)

申明:以上只限于参考学习交流!!!更多:https://github.com/huwei86/spiderlagou

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291