2016-11-17 63 views
0

我使用scrapy和硒進行爬行!我的網站使用阿賈克斯分頁!實際上,url沒有改變,所以response.body也沒有改變!我想點擊硒(分頁),並得到self.driver.page_source和使用它,而不是response.body! 所以我寫了這段代碼:更新scrapy中的響應(無需重新加載)

res = scrapy.http.TextResponse(url=self.driver.current_url, body=self.driver.page_source, 
              encoding='utf-8') 
      print(str(res)) //nothing to print! 

      for quote in res.css("#ctl00_ContentPlaceHolder1_Grd_Dr_DXMainTable > tr.dxgvDataRow_Office2003Blue"): 
       i = i+1 
       item = dict() 


       item['id'] = int(quote.css("td.dxgv:nth-child(1)::text").extract_first()) 

沒有錯誤!

回答

2

您可以通過使用response.replace()方法代替原有scrapy響應的身體:

def parse(self, response): 
    response = response.replace(body=driver.page_source) 
+0

我使用它,但不工作!沒有什麼比上次更改! – Mohsen

+0

@ user6935676它確實有效,你是否認爲response.body和driver.page_source中的新身體是相同的? – Granitosaurus

+0

很tnx!和PLZ,如果你可以回答我的其他[問題](http://stackoverflow.com/questions/40683617/webdriverwait-not-working-with-scrapy):) – Mohsen

相關問題