Uno, determine el contenido de rastreo y cree una tabla mysql
1. Determine la URL que se rastreará.
A través de la observación, puede encontrar que la URL es
https://search.51job.com/list/000000,000000,0000,32,9,99,+,2,xxxx.html ,
simplemente modifique el xxxx, puede lograr el rastreo de varias páginas web.
2. Los datos de la página web de 51job pueden obtener dinámicamente datos json, que son recibidos por las variables js y mostrados en la página web. Por lo tanto, al rastrear, debe analizar las variables en la etiqueta del script.
3. Determine el campo de rastreo, y luego cree una tabla mysql. La estructura de la tabla
mysql es la siguiente:
Dos, proyecto scrapy arrastrándose
(1) Trabajo de preparación:
1. Ejecute scrapy startproject qcwy, cree un proyecto scrapy
2. Ejecute scrapy genspider qcwyCrawler www.xxx.com, cree un archivo rastreador
(2), cambie el archivo de configuración del proyecto settings.py:
# Scrapy settings for qcwy project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
from fake_useragent import UserAgent
BOT_NAME = 'qcwy'
SPIDER_MODULES = ['qcwy.spiders']
NEWSPIDER_MODULE = 'qcwy.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = UserAgent().random # 生成随机请求头
# Obey robots.txt rules
ROBOTSTXT_OBEY = False # 不遵守robot协议
LOG_LEVEL = 'ERROR' # 只打印error级别的日志
ITEM_PIPELINES = {
'qcwy.pipelines.QcwyPipeline': 300,
} # 开启爬虫管道
(3) Cambie el archivo items.py y determine el campo de rastreo
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class QcwyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
company = scrapy.Field()
job_name = scrapy.Field()
salary = scrapy.Field()
requirement = scrapy.Field()
welfare = scrapy.Field()
(4) Cambie el archivo qcwyCrawler.py y escriba el código del rastreador
import scrapy
import json
from qcwy.items import QcwyItem
class QcwycrawlerSpider(scrapy.Spider):
name = 'qcwyCrawler'
# allowed_domains = ['www.xxx.com']
start_urls = [] # start_urls列表中的url会被scrapy自动请求
def __init__(self, **kwargs):
super().__init__(**kwargs)
url = 'https://search.51job.com/list/000000,000000,0000,32,9,99,+,2,{}.html' # 生成要爬取的网址
for i in range(1, 1001):
self.start_urls.append(url.format(i))
def parse(self, response): # 利用xpath和json解析爬取到的数据
json_str = response.xpath('/html/body/script[2]/text()').extract_first()[29:]
data = json.loads(json_str)
item = QcwyItem()
for row in data['engine_search_result']:
item['company'] = row['company_name']
item['job_name'] = row['job_name']
item['salary'] = row['providesalary_text']
item['requirement'] = ' '.join(row['attribute_text'])
item['welfare'] = row['jobwelf']
yield item
(5) Cree el paquete dbutil y el archivo de conexión, escriba la clase de conexión mysql
from pymysql import connect
class MysqlConnection:
host = '127.0.0.1'
port = 3306
user = 'root'
password = 'qwe12333'
db = 'study'
charset = 'utf8'
@classmethod
def getConnection(cls):
conn = None
try:
conn = connect(
host=cls.host,
port=cls.port,
user=cls.user,
password=cls.password,
db=cls.db,
charset=cls.charset
)
except Exception as e:
print(e)
return conn
@classmethod
def closeConnection(cls, conn):
if not conn:
conn.close()
(6) Cambie el archivo de canalización del rastreador y almacene los datos en la tabla mysql
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from dbutil.connection import MysqlConnection
from time import time
class QcwyPipeline:
def __init__(self):
self.start_time = time()
self.conn = MysqlConnection.getConnection()
self.sql = 'insert into qcwy values(null ,%s, %s, %s, %s, %s);'
self.count = 0
def process_item(self, item, spider):
self.cursor = self.conn.cursor()
company = item['company']
job_name = item['job_name']
salary = item['salary']
requirement = item['requirement']
welfare = item['welfare']
print('{}: {}'.format(company, job_name))
self.count += self.cursor.execute(self.sql, (company, job_name, salary, requirement, welfare))
def close_spider(self, spider):
if self.cursor:
self.cursor.close()
self.conn.commit()
MysqlConnection.closeConnection(self.conn)
print('总共爬取{}条记录,耗时:{}秒'.format(self.count, time() - self.start_time))
(7) Escriba el archivo main.py para iniciar el proyecto scrapy
from scrapy import cmdline
cmdline.execute('scrapy crawl qcwyCrawler'.split())
La estructura del proyecto creada es la siguiente:
resultado de la operación: