2016-07-27 81 views
0

我試圖找到一種方法來刮和解析更多頁面登錄區域。 這些示例鏈接可以從登錄我可以解析。如何在登錄後掃描頁面

#http://example.com/seller/demand/?id=305554 
#http://example.com/seller/demand/?id=305553 
#http://example.com/seller/demand/?id=305552 
#.... 

我想創建蜘蛛,可以打開這些鏈接中的每一個,然後解析它們。 我創建了另一隻蜘蛛,它可以打開並解析,其中只有一個是

當我試圖創建「for」或「while」來調用其他鏈接的更多請求時,它允許我不是因爲我不能將更多的返回到生成器,它返回錯誤。我也試過鏈接提取器,但它對我沒有用。

這裏是我的代碼:

#!c:/server/www/scrapy 
    # -*- coding: utf-8 -*- 
    from scrapy import Spider 
    from scrapy.selector import Selector 
    from scrapy.http import FormRequest 
    from scrapy.http.request import Request 
    from scrapy.spiders import CrawlSpider, Rule 
    from array import * 
    from stack.items import StackItem 
    from scrapy.linkextractors import LinkExtractor 

    class Spider3(Spider): 
     name = "Spider3" 
     allowed_domains = ["example.com"] 
     start_urls = ["http://example.com/login"] #this link lead to login page 

當我簽署了它返回一個URL頁面,包含「統計」,這就是爲什麼我第一次把這裏的「如果」條件。 當我登錄時,我請求一個鏈接和調用函數parse_items。

 def parse(self, response): 
       #when "stat" is in url it means that I just signed in 
       if "stat" in response.url:    
        return Request("http://example.com/seller/demand/?id=305554", callback = self.parse_items) 

      else: 
       #this succesful login turns me to page, it's url contains "stat" 
       return [FormRequest.from_response(response, 
         formdata={'ctl00$ContentPlaceHolder1$lMain$tbLogin': 'my_login', 'ctl00$ContentPlaceHolder1$lMain$tbPass': 'my_password'},callback=self.parse)] 

功能parse_items簡單解析需要的內容從一個期望頁:

 def parse_items(self,response): 
       questions = Selector(response).xpath('//*[@id="ctl00_ContentPlaceHolder1_cRequest_divAll"]/table/tr') 
       for question in questions: 
        item = StackItem() 
        item['name'] = question.xpath('th/text()').extract()[0] 
        item['value'] = question.xpath('td/text()').extract()[0] 
        yield item 

你能幫助我,請更新此代碼打開和分析比一頁更在每個會話? 我不想一遍又一遍地登錄每個請求。

+0

您好,歡迎計算器!請花一點時間閱讀本教程以獲得更好的問題,尤其是關於優秀和劣勢遊戲的示例:stackoverflow.com/help/how-to-ask - 其他所有內容都很不錯。 – Jurik

+0

你需要在這裏做的是找到下一頁的網址,並在你的'parse_items'方法中形成一個請求。例如'yield Request(next_page,callback = self.parse_items)'。該會話很可能取決於Cookie和Scrapy本身的管理。除此之外,如果你沒有提供你正在抓取的網址,我們無法真正幫助你。 – Granitosaurus

+0

您確定您的登錄成功嗎? –

回答

0

該會話最有可能取決於cookies和scrapy自己管理它。 I.e:

def parse_items(self,response): 
    questions = Selector(response).xpath('//*[@id="ctl00_ContentPlaceHolder1_cRequest_divAll"]/table/tr') 
    for question in questions: 
     item = StackItem() 
     item['name'] = question.xpath('th/text()').extract()[0] 
     item['value'] = question.xpath('td/text()').extract()[0] 
     yield item 
    next_url = '' # find url to next page in the current page 
    if next_url: 
     yield Request(next_url, self.parse_items) 
     # scrapy will retain the session for the next page if it's managed by cookies 
0

我目前正在研究同樣的問題。我使用InitSpider,所以我可以覆蓋__init__init_request。首先是隻爲自定義的東西初始化和實際魔術在我init_request情況:

def init_request(self): 
    """This function is called before crawling starts.""" 

    # Do not start a request on error, 
    # simply return nothing and quit scrapy 
    if self.abort: 
     return 

    # Do a login 
    if self.login_required: 
     # Start with login first 
     return Request(url=self.login_page, callback=self.login) 
    else: 
     # Start with pase function 
     return Request(url=self.base_url, callback=self.parse) 

我的登錄看起來像這樣

def login(self, response): 
    """Generate a login request.""" 

    self.log('Login called') 
    return FormRequest.from_response(
     response, 
     formdata=self.login_data, 
     method=self.login_method, 
     callback=self.check_login_response 
    ) 

self.login_datadict與提交值。

我仍然是Python和scrapy的初學者,所以我可能會做錯誤的方式。無論如何,到目前爲止,我已經制作了一個可以在github上查看的工作版本。

HTH:

https://github.com/cytopia/crawlpy