scrapy爬取珍爱网数据

mac2024-03-29  29

main.py

from scrapy import cmdline cmdline.execute(['scrapy','crawl','zhenai'])

settings.py

# -*- coding: utf-8 -*- # Scrapy settings for zhenaiwang 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 = 'zhenaiwang' SPIDER_MODULES = ['zhenaiwang.spiders'] NEWSPIDER_MODULE = 'zhenaiwang.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent #USER_AGENT = 'zhenaiwang (+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://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 = { # 'zhenaiwang.middlewares.ZhenaiwangSpiderMiddleware': 543, #} # Enable or disable downloader middlewares # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html #DOWNLOADER_MIDDLEWARES = { # 'zhenaiwang.middlewares.ZhenaiwangDownloaderMiddleware': 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 = { 'zhenaiwang.pipelines.ZhenaiwangPipeline': 300, } # 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'

items.py

# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://docs.scrapy.org/en/latest/topics/items.html import scrapy class ZhenaiwangItem(scrapy.Item): # define the fields for your item here like: name = scrapy.Field() age = scrapy.Field() local = scrapy.Field() sex = scrapy.Field() status = scrapy.Field() height = scrapy.Field() education = scrapy.Field()

pipelines.py

# -*- coding: utf-8 -*- # 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 import MySQLdb conn = MySQLdb.connect( host = 'localhost', port = 3306, user = 'root', password = '123456', db = 'spider', charset = 'utf8' ) cursor = conn.cursor() class ZhenaiwangPipeline(object): def process_item(self, item, spider): sql = 'insert into t_zhenai values (%s,%s,%s,%s,%s,%s,%s)' data = (item['name'],item['age'],item['local'],item['sex'],item['status'],item['height'],item['education']) cursor.execute(sql,data) conn.commit() return item

zhenai_spider.py

import scrapy from ..items import ZhenaiwangItem headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36', 'Cookie': 'sid=650e6f9b-337e-4f79-aa1d-e0e18073c2d1; Hm_lvt_2c8ad67df9e787ad29dbd54ee608f5d2=1572424230; __channelId=905819%2C0; oneclickLoginSwitch=2897; oneClickRegisterSwitch=2783; Hm_lpvt_2c8ad67df9e787ad29dbd54ee608f5d2=1572425293', 'Host': 'www.zhenai.com', } class ZhenaiSpider(scrapy.Spider): name = 'zhenai' def start_requests(self): url = 'http://www.zhenai.com/zhenghun/' yield scrapy.Request(url=url) def parse(self, response): urls = response.xpath('//*[@id="app"]/article/dl/dd/a/@href').getall() for url in urls: #获取6页数据 for i in range(1,7): url = url + '/' +str(i) yield scrapy.Request(url=url,headers=headers,callback=self.parse1) def parse1(self, response): name = response.xpath('//table/tbody/tr[1]/th/a/text()').getall() # sex = response.xpath('//table/tbody/tr[2]/td[1]').xpath('string(.)').getall() sex = response.xpath('//table/tbody/tr[2]/td[1]/text()').getall() local = response.xpath('//table/tbody/tr[2]/td[2]/text()').getall() # local = response.xpath('//table/tbody/tr[2]/td[2]').xpath('string(.)').getall() # age = response.xpath('//table/tbody/tr[3]/td[1]').xpath('string(.)').getall() age = response.xpath('//table/tbody/tr[3]/td[1]/text()').getall() education = response.xpath('//table/tbody/tr[3]/td[2]/text()').getall() # education = response.xpath('//table/tbody/tr[3]/td[2]').xpath('string(.)').getall() # status = response.xpath('//table/tbody/tr[4]/td[1]').xpath('string(.)').getall() status = response.xpath('//table/tbody/tr[4]/td[1]/text()').getall() height = response.xpath('//table/tbody/tr[4]/td[2]/text()').getall() # height = response.xpath('//table/tbody/tr[4]/td[2]').xpath('string(.)').getall() print(name,age,local,sex,status,height,education) print(len(sex),len(name),len(age),len(local),len(status),len(height),len(education)) for i in range(len(name)): info = ZhenaiwangItem() #对info字典进行赋值 info['name'] = name[i] info['age'] = age[i] info['local'] = local[i] info['sex'] = sex[i] info['status'] = status[i] info['height'] = height[i] info['education'] = education[i] yield info
最新回复(0)