创建scrapy文件
C:\Users\dell\PycharmProjects\TestDemo001\TestDemo001>scrapy startproject TestDemo001
scrapy genspider -t basic ganji "http://anqing.ganji.com/"
scrapy crawl ganji
存储csv
scrapy crawl ganji -o ganji.csv
爬赶集
ganji.py
# -*- coding: utf-8 -*-
import scrapy
from TestDemo001.items import Testdemo001Item
class GanjiSpider(scrapy.Spider):
name = 'ganji'
allowed_domains = ['http://anqing.ganji.com/']
start_urls = []
start_url = 'http://bj.ganji.com/site/s/f0/_%E7%A8%8B%E5%BA%8F%E5%91%98/'
for i in range(0,1985,32):
start_urls.append(start_url.replace("f0","f"+str(i)))
def parse(self, response):
node_list = response.xpath("//div[@class='job-wanted']/dl")
for node in node_list:
try:
ti = Testdemo001Item()
name = node.xpath("./dt[@class='f-introd']/a/text()").extract()[0]
addr = node.xpath("./dd[@class='addr']/a/text()").extract()[0]
time = node.xpath("./dd[@class='j-time']/text()").extract()[0]
ti["name"] = name
ti["addr"] = addr
ti["time"] = time
yield ti
except:
pass
import scrapy
class Testdemo001Item(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
addr = scrapy.Field()
time = scrapy.Field()
class Testdemo001Pipeline(object):
def process_item(self, item, spider):
# with open("text.txt","w+") as f:
#f.write(item)
if item ==None:
return None
else:
with open("text.txt","a+") as f:
f.write(item["name"]+item["addr"]+item["time"]+"\n")
return item
# -*- coding: utf-8 -*-
# Scrapy settings for TestDemo001 project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://doc.scrapy.org/en/latest/topics/settings.html
# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'TestDemo001'
SPIDER_MODULES = ['TestDemo001.spiders']
NEWSPIDER_MODULE = 'TestDemo001.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'TestDemo001 (+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 https://doc.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://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'TestDemo001.middlewares.Testdemo001SpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'TestDemo001.middlewares.Testdemo001DownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'TestDemo001.pipelines.Testdemo001Pipeline': 300,
}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://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 https://doc.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'