我目前在centos中使用scrapy + splash + python。我编写了以下代码,用于从here中提取内容。
无法从java脚本弹出窗口中提取数据,例如“href =”javascript:void(0);“任何人都指导我抓取。
蜘蛛代码:
def parse(self,response):
selector = Selector(response)
links = []
for link in selector.css('div.job_list div.cont > span.style.display:block;vertical-align: bottom;::attr(href)').extract():
yield Request(urlparse.urljoin(response.url, link),
callback=self.parse_listing_page,
#meta={"use_splash": False}
)
以下错误:
[testtim@dpitstsvr015 samsungcom]$ scrapy crawl samsung
2014-11-11 18:05:33+0530 [scrapy] INFO: Scrapy 0.24.4 started (bot: samsungcom)
2014-11-11 18:05:33+0530 [scrapy] INFO: Optional features available: ssl, http11
2014-11-11 18:05:33+0530 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'samsungcom.spiders', 'CONCURRENT_REQUESTS_PER_DOMAIN': 1, 'CONCURRENT_REQUESTS': 1, 'RETRY_ENABLED': False, 'SPIDER_MODULES': ['samsungcom.spiders'], 'BOT_NAME': 'samsungcom'}
2014-11-11 18:05:33+0530 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-11-11 18:05:33+0530 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, SplashMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-11-11 18:05:33+0530 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-11-11 18:05:33+0530 [scrapy] INFO: Enabled item pipelines:
2014-11-11 18:05:33+0530 [samsung] INFO: Spider opened
2014-11-11 18:05:33+0530 [samsung] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-11-11 18:05:33+0530 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2014-11-11 18:05:33+0530 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
https://careers.us.samsung.com/careers/svc/app/viewSearchJob
http://localhost:9999/render.html?url=https%3A%2F%2Fcareers.us.samsung.com%2Fcareers%2Fsvc%2Fapp%2FviewSearchJob
2014-11-11 18:05:58+0530 [samsung] DEBUG: Crawled (200) <GET http://localhost:9999/render.html?url=https%3A%2F%2Fcareers.us.samsung.com%2Fcareers%2Fsvc%2Fapp%2FviewSearchJob&wait=2&images=0> (referer: None)
2014-11-11 18:05:58+0530 [samsung] ERROR: Spider error processing <GET http://localhost:9999/render.html?url=https%3A%2F%2Fcareers.us.samsung.com%2Fcareers%2Fsvc%2Fapp%2FviewSearchJob&wait=2&images=0>
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 824, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 638, in _tick
taskObj._oneWorkUnit()
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 484, in _oneWorkUnit
result = next(self._iterator)
File "/usr/lib64/python2.7/site-packages/scrapy/utils/defer.py", line 57, in <genexpr>
work = (callable(elem, *args, **named) for elem in iterable)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/scrapy/utils/defer.py", line 96, in iter_errback
yield next(it)
File "/usr/lib64/python2.7/site-packages/scrapy/contrib/spidermiddleware/offsite.py", line 26, in process_spider_output
for x in result:
File "/usr/lib64/python2.7/site-packages/scrapy/contrib/spidermiddleware/referer.py", line 22, in <genexpr>
return (_set_referer(r) for r in result or ())
File "/usr/lib64/python2.7/site-packages/scrapy/contrib/spidermiddleware/urllength.py", line 33, in <genexpr>
return (r for r in result or () if _filter(r))
File "/usr/lib64/python2.7/site-packages/scrapy/contrib/spidermiddleware/depth.py", line 50, in <genexpr>
return (r for r in result or () if _filter(r))
File "/home/sureshp/Downloads/samsungcom/samsungcom/spiders/samsung.py", line 24, in parse
for link in selector.css('div.job_list div.cont > span.style.display:block;vertical-align: bottom;::attr(href)').extract():
File "/usr/lib64/python2.7/site-packages/scrapy/selector/unified.py", line 110, in css
return self.xpath(self._css2xpath(query))
File "/usr/lib64/python2.7/site-packages/scrapy/selector/unified.py", line 113, in _css2xpath
return self._csstranslator.css_to_xpath(query)
File "/usr/lib/python2.7/site-packages/cssselect/xpath.py", line 192, in css_to_xpath
for selector in parse(css))
File "/usr/lib/python2.7/site-packages/cssselect/parser.py", line 355, in parse
return list(parse_selector_group(stream))
File "/usr/lib/python2.7/site-packages/cssselect/parser.py", line 370, in parse_selector_group
yield Selector(*parse_selector(stream))
File "/usr/lib/python2.7/site-packages/cssselect/parser.py", line 396, in parse_selector
next_selector, pseudo_element = parse_simple_selector(stream)
File "/usr/lib/python2.7/site-packages/cssselect/parser.py", line 477, in parse_simple_selector
**"Expected selector, got %s" % (peek,))
cssselect.parser.SelectorSyntaxError: Expected selector, got <DELIM ';' at 48>**
2014-11-11 18:05:58+0530 [samsung] INFO: Closing spider (finished)
2014-11-11 18:05:58+0530 [samsung] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 314,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 111927,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 11, 11, 12, 35, 58, 732318),
'log_count/DEBUG': 3,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'spider_exceptions/SelectorSyntaxError': 1,
'splash/unwrapped': 1,
'splash/wrapped': 1,
'start_time': datetime.datetime(2014, 11, 11, 12, 35, 33, 902315)}
2014-11-11 18:05:58+0530 [samsung] INFO: Spider closed (finished)
答案 0 :(得分:2)
div.job_list div.cont > span.style.display:block;vertical-align: bottom;::attr(href)
不是有效的CSS选择器。
尝试:
'div.job_list div.cont > span[style="display:block;vertical-align: bottom;"]::attr(href)'
请注意::attr(href)
是CSS选择器的Scrapy扩展,用于使用伪元素语法选择属性值。