2013-04-30 66 views
3

我想抓取保險網站www.ehealthinsurance.com。 它的主頁有一個POST依賴窗體,其中需要一定的值並生成下一頁。我試圖通過值,但無法看到我想要的標籤的HTML源代碼。任何建議都會有很大的幫助。如何使用scrapy抓取帖子相關網站

內聯是scrapy代碼:

class ehealthSpider(BaseSpider): 
    name = "ehealth" 
    allowed_domains = ["ehealthinsurance.com/"] 
    start_urls = ["http://www.ehealthinsurance.com/individual-health-insurance"] 

    def parse(self, response): 
     yield FormRequest.from_response(response, 
             formname='main', 
             formdata={'census.zipCode': '48341', 
                'census.requestEffectiveDate': '06/01/2013', 
                'census.primary.gender': 'MALE', 
                'census.primary.month': '12', 
                'census.primary.day': '01', 
                'census.primary.year': '1971', 
                'census.primary.tobacco': 'No', 
                'census.primary.student': 'No'}, callback=self.parseAnnonces) 

    def parseAnnonces(self, response): 
     hxs = HtmlXPathSelector(response) 
     data = hxs.select('//div[@class="main-wrap"]').extract() 
     #print encoding 
     print data 

這是履帶式的終端響應

2013-04-30 16:34:16+0530 [elyse] DEBUG: Crawled (200) <GET http://www.ehealthin 
    urance.com/individual-health-insurance> (referer: None) 
    2013-04-30 16:34:17+0530 [elyse] DEBUG: Filtered offsite request to 'www.ehealt 
    insurance.com': <POST http://www.ehealthinsurance.com/individual-health-insuran 
    e;jsessionid=F5A1123CE731FDDDC1A7A31CD46CC132.prfo23a> 
    2013-04-30 16:34:17+0530 [elyse] INFO: Closing spider (finished) 
    2013-04-30 16:34:17+0530 [elyse] INFO: Dumping Scrapy stats: 
    {'downloader/request_bytes': 257, 
    'downloader/request_count': 1, 
    'downloader/request_method_count/GET': 1, 
    'downloader/response_bytes': 32561, 
    'downloader/response_count': 1, 
    'downloader/response_status_count/200': 1, 
    'finish_reason': 'finished', 
    'finish_time': datetime.datetime(2013, 4, 30, 11, 4, 17, 22000), 
    'log_count/DEBUG': 8, 
    'log_count/INFO': 4, 
    'request_depth_max': 1, 
    'response_received_count': 1, 
    'scheduler/dequeued': 1, 
    'scheduler/dequeued/memory': 1, 
    'scheduler/enqueued': 1, 
    'scheduler/enqueued/memory': 1, 
    'start_time': datetime.datetime(2013, 4, 30, 11, 4, 10, 494000)} 

能否請你幫我得到想要的數據?

+0

問題是你試圖抓取頁面的上映之前:有在中間的頁面加載 - 提交表單後顯示結果之前,加重定向。看起來很難刮。 – alecxe 2013-04-30 21:30:57

+0

謝謝alecxe。我研究了相同的解決方法,我知道的是硒與scrapy。但不如何如何解析硒加載頁面scrapy?任何幫助讚賞 – Tarun 2013-05-13 11:48:29

回答

1

中間請求的小技巧,它的工作原理。 也修正了formname。 Scrapy的大調試工具是inspect_response(response)

from scrapy.selector import Selector 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from healthinsspider.items import HealthinsspiderItem 
from scrapy.shell import inspect_response 
from scrapy.http import FormRequest 
from scrapy.http import Request 
import time 

class EhealthspiderSpider(CrawlSpider): 
    name = 'ehealthSpider' 
    allowed_domains = ['ehealthinsurance.com'] 
    start_urls = ["http://www.ehealthinsurance.com/individual-health-insurance"] 

    def parse(self, response): 
     yield FormRequest.from_response(response, 
             formname='form-census', 
             formdata={'census.zipCode': '48341', 
                'census.requestEffectiveDate': '06/01/2013', 
                'census.primary.gender': 'MALE', 
                'census.primary.month': '12', 
                'census.primary.day': '01', 
                'census.primary.year': '1971', 
                'census.primary.tobacco': 'No', 
                'census.primary.student': 'No'}, callback=self.InterRequest, 
                dont_filter=True) 

    def InterRequest(self, response): 
     # sleep so, that our request can be processed by the server, than go to results 
     time.sleep(10) 
     return Request(url='https://www.ehealthinsurance.com/ehi/ifp/individual-family-health-insurance!goToScreen?referer=https%3A%2F%2Fwww.ehealthinsurance.com%2Fehi%2Fifp%2Findividual-health-insurance%3FredirectFormHTTP&sourcePage=&edit=false&ajax=false&screenName=best-sellers', dont_filter=True, callback=self.parseAnnonces) 

    def parseAnnonces(self, response): 
     inspect_response(response) 
     hxs = Selector(response) 
     data = hxs.select('//div[@class="main-wrap"]').extract() 
     #print encoding 
     print data 

P.S.餅乾應在settings.py啓用:COOKIES_ENABLED=True