2014-12-05 2 views

답변

11

먼저, 당신은 Scrapy shell 사용할 수 있습니다

$ cat index.html 
<div id="test"> 
    Test text 
</div> 

$ scrapy shell index.html 
>>> response.xpath('//div[@id="test"]/text()').extract()[0].strip() 
u'Test text' 

different objects available in the shell 있습니다 세션 동안, responserequest처럼.


또는 당신이 HtmlResponse class를 인스턴스화하고 body에 HTML 문자열을 제공 할 수 있습니다

>>> from scrapy.http import HtmlResponse 
>>> response = HtmlResponse(url="my HTML string", body='<div id="test">Test text</div>') 
>>> response.xpath('//div[@id="test"]/text()').extract()[0].strip() 
u'Test text' 
+0

덕분에 나는 약간 ajaxiness의 becuase 셀레늄을 사용하고, alecxe. driver.page_source를 resposne과 같은 객체로 변환하여 lxml에 의존하지 않고 일부 추출기 (CSS 및 xpath 선택기 사용)를 재사용 할 수 있습니다. 두 번째 옵션은 내가 필요한 것 같아. – yayu

+1

@yayu 그런 다음 HTML 응답을 만들 필요는 없지만, '선택자'는 http://stackoverflow.com/questions/18836286/scraping-with-scrapy-and-selenium과 http : //stackoverflow.com/questions/17975471/selenium-with-scrapy-for-dynamic-page. 도움이 될지도 몰라. 감사. – alecxe

+0

감사합니다. 나는 그것을 들여다 볼 것이다. – yayu

관련 문제