变更
@@ -0,0 +1,14 @@
|
||||
# Define here the models for your scraped items
|
||||
#
|
||||
# See documentation in:
|
||||
# https://docs.scrapy.org/en/latest/topics/items.html
|
||||
|
||||
import scrapy
|
||||
|
||||
|
||||
class MeinvItem(scrapy.Item):
|
||||
# define the fields for your item here like:
|
||||
name = scrapy.Field()
|
||||
img = scrapy.Field()
|
||||
local_path = scrapy.Field()
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# Define here the models for your spider middleware
|
||||
#
|
||||
# See documentation in:
|
||||
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
|
||||
|
||||
from scrapy import signals
|
||||
|
||||
# useful for handling different item types with a single interface
|
||||
from itemadapter import is_item, ItemAdapter
|
||||
|
||||
|
||||
class MeinvSpiderMiddleware:
|
||||
# Not all methods need to be defined. If a method is not defined,
|
||||
# scrapy acts as if the spider middleware does not modify the
|
||||
# passed objects.
|
||||
|
||||
@classmethod
|
||||
def from_crawler(cls, crawler):
|
||||
# This method is used by Scrapy to create your spiders.
|
||||
s = cls()
|
||||
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
|
||||
return s
|
||||
|
||||
def process_spider_input(self, response, spider):
|
||||
# Called for each response that goes through the spider
|
||||
# middleware and into the spider.
|
||||
|
||||
# Should return None or raise an exception.
|
||||
return None
|
||||
|
||||
def process_spider_output(self, response, result, spider):
|
||||
# Called with the results returned from the Spider, after
|
||||
# it has processed the response.
|
||||
|
||||
# Must return an iterable of Request, or item objects.
|
||||
for i in result:
|
||||
yield i
|
||||
|
||||
def process_spider_exception(self, response, exception, spider):
|
||||
# Called when a spider or process_spider_input() method
|
||||
# (from other spider middleware) raises an exception.
|
||||
|
||||
# Should return either None or an iterable of Request or item objects.
|
||||
pass
|
||||
|
||||
def process_start_requests(self, start_requests, spider):
|
||||
# Called with the start requests of the spider, and works
|
||||
# similarly to the process_spider_output() method, except
|
||||
# that it doesn’t have a response associated.
|
||||
|
||||
# Must return only requests (not items).
|
||||
for r in start_requests:
|
||||
yield r
|
||||
|
||||
def spider_opened(self, spider):
|
||||
spider.logger.info("Spider opened: %s" % spider.name)
|
||||
|
||||
|
||||
class MeinvDownloaderMiddleware:
|
||||
# Not all methods need to be defined. If a method is not defined,
|
||||
# scrapy acts as if the downloader middleware does not modify the
|
||||
# passed objects.
|
||||
|
||||
@classmethod
|
||||
def from_crawler(cls, crawler):
|
||||
# This method is used by Scrapy to create your spiders.
|
||||
s = cls()
|
||||
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
|
||||
return s
|
||||
|
||||
def process_request(self, request, spider):
|
||||
# Called for each request that goes through the downloader
|
||||
# middleware.
|
||||
|
||||
# Must either:
|
||||
# - return None: continue processing this request
|
||||
# - or return a Response object
|
||||
# - or return a Request object
|
||||
# - or raise IgnoreRequest: process_exception() methods of
|
||||
# installed downloader middleware will be called
|
||||
return None
|
||||
|
||||
def process_response(self, request, response, spider):
|
||||
# Called with the response returned from the downloader.
|
||||
|
||||
# Must either;
|
||||
# - return a Response object
|
||||
# - return a Request object
|
||||
# - or raise IgnoreRequest
|
||||
return response
|
||||
|
||||
def process_exception(self, request, exception, spider):
|
||||
# Called when a download handler or a process_request()
|
||||
# (from other downloader middleware) raises an exception.
|
||||
|
||||
# Must either:
|
||||
# - return None: continue processing this exception
|
||||
# - return a Response object: stops process_exception() chain
|
||||
# - return a Request object: stops process_exception() chain
|
||||
pass
|
||||
|
||||
def spider_opened(self, spider):
|
||||
spider.logger.info("Spider opened: %s" % spider.name)
|
||||
@@ -0,0 +1,60 @@
|
||||
# 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 scrapy.pipelines.images import ImagesPipeline
|
||||
import scrapy
|
||||
import pymysql
|
||||
from meinv.settings import MYSQL
|
||||
|
||||
|
||||
class MeinvPipeline:
|
||||
def open_spider(self, spider):
|
||||
print("数据爬取开始")
|
||||
self.conn = pymysql.connect(
|
||||
host=MYSQL["host"],
|
||||
port=MYSQL["port"],
|
||||
user=MYSQL["user"],
|
||||
password=MYSQL["password"],
|
||||
database=MYSQL["database"]
|
||||
)
|
||||
|
||||
def close_spider(self, spider):
|
||||
print("数据爬取结束")
|
||||
self.conn.close()
|
||||
|
||||
def process_item(self, item, spider):
|
||||
# mysql写入
|
||||
try:
|
||||
cursor = self.conn.cursor()
|
||||
sql = "insert into tu (name, img) values (%s,%s)"
|
||||
cursor.execute(sql, (item['name'], item["local_path"]))
|
||||
self.conn.commit()
|
||||
print("写入成功")
|
||||
except:
|
||||
self.conn.rollback()
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
return item
|
||||
|
||||
|
||||
# 想使用image需要在setting中设置存放位置IMAGES_STORE
|
||||
class MeinvSavePipeline(ImagesPipeline): # 利用图片管道下载
|
||||
def get_media_requests(self, item, info): # 负责下载
|
||||
return scrapy.Request(item["img"]) # 直接返回结果
|
||||
|
||||
def file_path(self, request, response=None, info=None, *, item=None): # 准备文件路径
|
||||
file_name = request.url.split("/")[-1]
|
||||
return f"img/{file_name}" # 创建img/***
|
||||
|
||||
def item_completed(self, results, item, info): # 返回文件详细信息
|
||||
ok, finfo = results[0]
|
||||
path = finfo['path']
|
||||
item["local_path"] = path
|
||||
print(results)
|
||||
return item
|
||||
@@ -0,0 +1,100 @@
|
||||
# Scrapy settings for meinv 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
|
||||
|
||||
BOT_NAME = "meinv"
|
||||
|
||||
SPIDER_MODULES = ["meinv.spiders"]
|
||||
NEWSPIDER_MODULE = "meinv.spiders"
|
||||
LOG_LEVEL = "WARNING"
|
||||
|
||||
# Crawl responsibly by identifying yourself (and your website) on the user-agent
|
||||
# USER_AGENT = "meinv (+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 https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
|
||||
# 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 = {
|
||||
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
# "Accept-Language": "en",
|
||||
# }
|
||||
|
||||
# Enable or disable spider middlewares
|
||||
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
|
||||
# SPIDER_MIDDLEWARES = {
|
||||
# "meinv.middlewares.MeinvSpiderMiddleware": 543,
|
||||
# }
|
||||
|
||||
# Enable or disable downloader middlewares
|
||||
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
|
||||
# DOWNLOADER_MIDDLEWARES = {
|
||||
# "meinv.middlewares.MeinvDownloaderMiddleware": 543,
|
||||
# }
|
||||
|
||||
# Enable or disable extensions
|
||||
# See https://docs.scrapy.org/en/latest/topics/extensions.html
|
||||
# EXTENSIONS = {
|
||||
# "scrapy.extensions.telnet.TelnetConsole": None,
|
||||
# }
|
||||
|
||||
# Configure item pipelines
|
||||
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
|
||||
ITEM_PIPELINES = {
|
||||
"meinv.pipelines.MeinvPipeline": 300,
|
||||
"meinv.pipelines.MeinvSavePipeline": 280,
|
||||
}
|
||||
IMAGES_STORE = './meinvtupian'
|
||||
# Enable and configure the AutoThrottle extension (disabled by default)
|
||||
# See https://docs.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 https://docs.scrapy.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"
|
||||
MYSQL = {
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"user": "root",
|
||||
"password": "123123",
|
||||
"database": "spider"
|
||||
}
|
||||
# Set settings whose default value is deprecated to a future-proof value
|
||||
REQUEST_FINGERPRINTER_IMPLEMENTATION = "2.7"
|
||||
TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
|
||||
FEED_EXPORT_ENCODING = "utf-8"
|
||||
@@ -0,0 +1,4 @@
|
||||
# This package will contain the spiders of your Scrapy project
|
||||
#
|
||||
# Please refer to the documentation for information on how to create and manage
|
||||
# your spiders.
|
||||
@@ -0,0 +1,36 @@
|
||||
import scrapy
|
||||
from meinv.items import MeinvItem
|
||||
|
||||
|
||||
class MeinvSpiderSpider(scrapy.Spider):
|
||||
name = "meinv_spider"
|
||||
allowed_domains = ["www.tpzj.com"]
|
||||
start_urls = ["https://www.tpzj.com/meinv/xiezhen/"]
|
||||
|
||||
def parse(self, response):
|
||||
li_list = response.xpath("//ul[@class='list_con_box_ul']/li")
|
||||
for li in li_list:
|
||||
href = li.xpath("./a/@href").extract_first()
|
||||
yield scrapy.Request(
|
||||
url=response.urljoin(href), # href不是完整的url地址,使用urljoin拼接url
|
||||
method='get',
|
||||
callback=self.parse_detail # 回调函数,当相应回馈后,如何进行相应处理
|
||||
)
|
||||
# print(response.urljoin(href))
|
||||
# 下一页
|
||||
next_href = response.xpath(
|
||||
"//div[@class='pages']/ul/li/a[contains(text(), '下一页')]/@href").extract_first() # contains(text(),"")判断文字是否包含内容
|
||||
if next_href:
|
||||
yield scrapy.Request(
|
||||
url=response.urljoin(next_href),
|
||||
callback=self.parse
|
||||
)
|
||||
|
||||
def parse_detail(self, response):
|
||||
name = response.xpath('//div[@class="list_con bgff"]/h1/text()').extract_first() # 没有first 会返回一个列表,导致报错
|
||||
img_src = response.xpath("//div[@id='bigpic']/a/img/@src").extract_first()
|
||||
item = MeinvItem()
|
||||
item["name"] = name
|
||||
item["img"] = img_src
|
||||
print(item)
|
||||
yield item
|
||||
|
After Width: | Height: | Size: 378 KiB |
|
After Width: | Height: | Size: 202 KiB |
|
After Width: | Height: | Size: 728 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 482 KiB |
|
After Width: | Height: | Size: 241 KiB |
|
After Width: | Height: | Size: 149 KiB |
|
After Width: | Height: | Size: 472 KiB |
|
After Width: | Height: | Size: 281 KiB |
|
After Width: | Height: | Size: 368 KiB |
|
After Width: | Height: | Size: 217 KiB |
|
After Width: | Height: | Size: 228 KiB |
|
After Width: | Height: | Size: 189 KiB |
|
After Width: | Height: | Size: 308 KiB |
|
After Width: | Height: | Size: 301 KiB |
|
After Width: | Height: | Size: 287 KiB |
|
After Width: | Height: | Size: 219 KiB |
|
After Width: | Height: | Size: 179 KiB |
|
After Width: | Height: | Size: 178 KiB |
|
After Width: | Height: | Size: 167 KiB |
|
After Width: | Height: | Size: 154 KiB |
|
After Width: | Height: | Size: 191 KiB |
|
After Width: | Height: | Size: 201 KiB |
|
After Width: | Height: | Size: 258 KiB |
|
After Width: | Height: | Size: 69 KiB |
|
After Width: | Height: | Size: 192 KiB |
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 132 KiB |
|
After Width: | Height: | Size: 184 KiB |
|
After Width: | Height: | Size: 265 KiB |
|
After Width: | Height: | Size: 302 KiB |
|
After Width: | Height: | Size: 226 KiB |
|
After Width: | Height: | Size: 269 KiB |
|
After Width: | Height: | Size: 231 KiB |
|
After Width: | Height: | Size: 302 KiB |
|
After Width: | Height: | Size: 201 KiB |
|
After Width: | Height: | Size: 293 KiB |
|
After Width: | Height: | Size: 186 KiB |
|
After Width: | Height: | Size: 302 KiB |
|
After Width: | Height: | Size: 162 KiB |
|
After Width: | Height: | Size: 173 KiB |
|
After Width: | Height: | Size: 239 KiB |
|
After Width: | Height: | Size: 286 KiB |
|
After Width: | Height: | Size: 188 KiB |
|
After Width: | Height: | Size: 170 KiB |
|
After Width: | Height: | Size: 380 KiB |
|
After Width: | Height: | Size: 239 KiB |
|
After Width: | Height: | Size: 260 KiB |
|
After Width: | Height: | Size: 308 KiB |
|
After Width: | Height: | Size: 352 KiB |
|
After Width: | Height: | Size: 269 KiB |
|
After Width: | Height: | Size: 194 KiB |
|
After Width: | Height: | Size: 136 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 436 KiB |
|
After Width: | Height: | Size: 197 KiB |
|
After Width: | Height: | Size: 273 KiB |
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 120 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 164 KiB |
|
After Width: | Height: | Size: 89 KiB |
|
After Width: | Height: | Size: 101 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 108 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 110 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 143 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 96 KiB |
|
After Width: | Height: | Size: 133 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 112 KiB |
|
After Width: | Height: | Size: 104 KiB |
|
After Width: | Height: | Size: 117 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 212 KiB |