我正在开发一个抓取https://e27.co/网站的网络爬虫。我使用了 scrapy-splash python 库和docker。
问题是scrapy只渲染url列表的最后一个url。
此外,当我打开 时response.body
,页面会自动将我重定向到“哎呀”页面。
我试图停止/使飞溅不遵循重定向,但它失败了。
这是我的蜘蛛:
import scrapy
from scrapy_splash import SplashRequest
import csv
class e27_spider(scrapy.Spider):
name = 'e27'
start_urls = ['http://www.e27.co/startup/uber', 'http://www.e27.co/startup/urban-ladder']
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url = url, callback = self.parse, args={'http_method': 'POST','follow_redirects':False})
# yield scrapy.Request(url, self.parse, meta={'splash':{'args':{'html':1, 'png':1,'dont_process_response':True}},'handle_httpstatus_list':[302],'dont_redirect':True})
def parse(self, response):
filename = 'asd-%s.html' % response.xpath('//*[@id="page-container"]/div[3]/div/div/div/div/div/div[2]/div[1]/div/h1/text()').extract_first()
with open(filename, 'wb') as f:
f.write(response.body)
final_data = {}
startup_name = response.xpath('//*[@id="page-container"]/div[3]/div/div/div/div/div/div[2]/div[1]/div/h1/text()').extract_first()
startup_founding_date = response.xpath('//*[@id="page-container"]/div[3]/div/div/div/div/div/div[2]/div[1]/div/p[3]/span/text()').extract_first()
startup_description = response.xpath('//*[@id="page-container"]/div[4]/div/div/div/div/div[1]/div[1]/div[1]/div/p/text()').extract_first()
final_data['Name'] = startup_name
final_data['Founding Date'] = startup_founding_date
final_data['Description'] = startup_description
print (final_data)
设置.py:
BOT_NAME = 'webcrawler'
SPIDER_MODULES = ['webcrawler.spiders']
NEWSPIDER_MODULE = 'webcrawler.spiders'
SPLASH_URL = 'http://192.168.99.100:8050'
DOWNLOADER_MIDDLEWARES = {
'scrapy_splash.SplashCookiesMiddleware': 723,
'scrapy_splash.SplashMiddleware': 725,
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}
SPIDER_MIDDLEWARES = {
'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'
以下是日志:
2017-06-27 01:04:53 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'webcrawler', 'DUPEFILTER_CLASS': 'scrapy_splash.SplashAwareDupeFilter', 'HTTPCACHE_STORAGE': 'scrapy_splash.SplashAwareFSCacheStorage', 'NEWSPIDER_MODULE': 'webcrawler.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['webcrawler.spiders']}
2017-06-27 01:04:53 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2017-06-27 01:04:53 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy_splash.SplashCookiesMiddleware',
'scrapy_splash.SplashMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-06-27 01:04:53 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy_splash.SplashDeduplicateArgsMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-06-27 01:04:53 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-06-27 01:04:53 [scrapy.core.engine] INFO: Spider opened
2017-06-27 01:04:53 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-06-27 01:04:53 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-06-27 01:04:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.e27.co/robots.txt> (referer: None)
2017-06-27 01:04:54 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://192.168.99.100:8050/robots.txt> (referer: None)
2017-06-27 01:04:54 [scrapy.core.engine] DEBUG: Crawled (200) <POST http://www.e27.co/startup/urban-ladder via http://192.168.99.100:8050/render.html> (referer: None)
{'Name': None, 'Founding Date': None, 'Description': None}
2017-06-27 01:04:57 [scrapy.core.engine] DEBUG: Crawled (200) <POST http://www.e27.co/startup/uber via http://192.168.99.100:8050/render.html> (referer: None)
{'Name': 'Uber', 'Founding Date': None, 'Description': '\n\t\t\t\t\t\t\t\t\t\t\t\tUber is a technology company that is changing the way the world thinks about transportation. They are building technology people use every day. Whether it’s heading home from work, getting a meal delivered from a favorite restaurant, or a way to earn extra income, Uber is becoming part of the fabric of daily life.'}
2017-06-27 01:04:57 [scrapy.core.engine] INFO: Closing spider (finished)
2017-06-27 01:04:57 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1876,
'downloader/request_count': 4,
'downloader/request_method_count/GET': 2,
'downloader/request_method_count/POST': 2,
'downloader/response_bytes': 192303,
'downloader/response_count': 4,
'downloader/response_status_count/200': 3,
'downloader/response_status_count/404': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 6, 26, 17, 4, 57, 366774),
'log_count/DEBUG': 5,
'log_count/INFO': 7,
'response_received_count': 4,
'scheduler/dequeued': 4,
'scheduler/dequeued/memory': 4,
'scheduler/enqueued': 4,
'scheduler/enqueued/memory': 4,
'splash/render.html/request_count': 2,
'splash/render.html/response_count/200': 2,
'start_time': datetime.datetime(2017, 6, 26, 17, 4, 53, 892873)}
2017-06-27 01:04:57 [scrapy.core.engine] INFO: Spider closed (finished)
知道为什么我的启动画面只呈现列表的最后一个 url 吗?