简体   繁体   中英

Scrapy recursive parse : what i am doing wrong here

I am trying to scrape aspx websites list view , hence structure of each page will be same and ( hence i am using recursive spider call's)

Error: ERROR: Spider must return Request, BaseItem or None, got 'list'

not sure what this error means ..

I am doing something wrong , very basic but can't identify ...point me in the right direction..Thanks

My Code:

    name = "XYZscraper"
allowed_domains = ["xyz.com"]

def __init__(self):
    self.start_urls = [
        "xyz.com with aspx list viwe",
    ]

def parse(self, response):
    sel = Selector(response)

    if sel.xpath('//table/tr/td/form/table/tr'):
        print "xpath is present"
        elements = sel.xpath('//table/tr/td/form/table/tr')
    else:
        print "xpath not present "
        print " going in with fallback xpath"
        elements = sel.xpath('///table/tr')
    counter = 1
    nextPageAvailable = False # flat if netx page link is available or not

    base_url = "xyz.com/"
    try:
        items = response.meta['item']
    except Exception as e:
        items = []
        pass

    no_of_row = len(elements)
    for each_row in elements:
        #first two row and last two row does not have data
        #first and last row have link to previous and next page ...using first row for navigation
        if counter == 1:
            if each_row.xpath('td/a[1]/text()').extract()[0] == "Previous":
                if each_row.xpath('td/a[2]/text()'):
                    if each_row.xpath('td/a[2]/text()').extract()[0] == "Next":
                        nextPageAvailable = True
            elif each_row.xpath('td/a[1]/text()').extract()[0] == "Next":
                nextPageAvailable = True
        if counter > 2:
            if counter < (no_of_row - 1):
                item = myItem()
                item['title'] = each_row.xpath('td/div/a/span/text()').extract()[0].encode('ascii', 'ignore')  # Title
                items.append(item)
        counter += 1
    if nextPageAvailable:
        yield FormRequest.from_response(
            response,
            meta={'item': items},
            formnumber=1,
            formdata={
                '__EVENTTARGET': 'ctl00$ctl10$EventsDG$ctl01$ctl01', #for request to navigate to next page in table
            },
            callback=self.parse  # calling recursive function since signature of page will remain same just data is refreshed
        )
    else:
        # when end of the list is arrived ...calling next functin to pop item ..may be !! does not work !!
        self.popItems(response)

        # does not work
        # Error: python < 3.3 does not allow return with argument inside the generator
        # return item


def popItems(self, response):
    print "i am here"
    items = ()
    baseitem = response.meta['item']
    items = baseitem
    return items

Maybe you mean something like this:

else:
    for item in self.popItems(response):
        yield item

Or the shorter version:

else:
    yield from self.popItems(response)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM