2016-04-26 176 views

回答

6

您的閱讀正確無誤,您最好的選擇是使用Firefox瀏覽器或無頭像PhantomJS的scrapy + selenium,以加快抓取速度。改編自https://stackoverflow.com/a/17979285/2781701

import scrapy 
from selenium import webdriver 

class ProductSpider(scrapy.Spider): 
    name = "product_spider" 
    allowed_domains = ['visit.rio'] 
    start_urls = ['http://visit.rio/en/o-que-fazer/outdoors'] 

    def __init__(self): 
     self.driver = webdriver.Firefox() 
    def parse(self, response): 
     self.driver.get(response.url) 

     while True: 
      next = self.driver.find_element_by_xpath('//div[@id="show_more"]/a') 

      try: 
       next.click() 

       # get the data and write it to scrapy items 
      except: 
       break 

     self.driver.close() 

實施例