鍍金池/ 問答/人工智能  Python  數(shù)據(jù)庫/ scrapy中callback無用,已閱讀seg上相關(guān)問題,沒能解決,望您解答

scrapy中callback無用,已閱讀seg上相關(guān)問題,沒能解決,望您解答

問題描述

爬取亞馬遜商品列表,將數(shù)據(jù)存入mongodb
爬取第一頁之后將下一頁鏈接傳入Request中,在shell中可以得到下一頁鏈接
然而在數(shù)據(jù)庫中只能看見第一頁的數(shù)據(jù)
在命令行中可以看到爬取完第一頁數(shù)據(jù)之后,有下一頁的鏈接出現(xiàn),卻沒有進行數(shù)據(jù)爬取

問題出現(xiàn)的平臺版本及自己嘗試過哪些方法

linux,mongodb
嘗試在Request中加入dont_filter=Ture
并沒有成功而且還爬了一些不需要的東西

相關(guān)代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)

spider.py

from scrapy import Request, Spider
from amazon.items import AmazonItem
class AmazonSpider(Spider):

name = 'book'
allowed_domains = ['amazon.com']
start_urls = ['https://www.amazon.com/s/ref=lp_2649512011_il_ti_movies-tv?rh=n%3A2625373011%2Cn%3A%212625374011%2Cn%3A2649512011&ie=UTF8&qid=1533351160&lo=movies-tv']


def parse(self, response):
    result = response.xpath('//div[@id="mainResults"]/ul/li')
    # print(result)
    for it in result:
        item = AmazonItem()
        item['title'] = it.css('h2::text').extract_first()
        item['price'] = it.css('.a-link-normal.a-text-normal .a-offscreen::text').extract_first()
        yield item

    next_page = response.css('#bottomBar #pagn #pagnNextLink::attr("href")').extract_first()
    url = response.urljoin('https://www.amazon.com',next_page)
    yield Request(url=url, callback=self.parse, dont_filter=True)

pipelines.py:

class MongoPipeline(object):

def __init__(self, mongo_uri, mongo_db):
    self.mongo_uri = mongo_uri        
    self.mongo_db = mongo_db



@classmethod
def from_crawler(cls, crawler):
    return cls(
        mongo_uri = crawler.settings.get('MONGO_URI'),
        mongo_db = crawler.settings.get('MONGO_DB')
        )


def open_spider(self, spider):
    self.client = pymongo.MongoClient(self.mongo_uri)
    self.db = self.client[self.mongo_db]


def process_item(self, item, spider):
    self.db[item.collection].insert(dict(item))
    return item

def close_spider(self, spider):
    self.client.close()

你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?

想要在url傳入Request之后能夠callback到parse中,進行下一頁的相關(guān)內(nèi)容的爬取
在命令行中可以看到爬取完第一頁數(shù)據(jù)之后,有下一頁的鏈接出現(xiàn),卻沒有進行數(shù)據(jù)爬取
而且鏈接復(fù)制到瀏覽器可以打開而且就是第2.。3.4.。之后的頁面
但是不知道為什么沒有進行數(shù)據(jù)爬取
圖片描述

希望大佬們可以不吝賜教,萬謝!

回答
編輯回答
毀與悔

昨天又試了一遍
到第二頁的頁面中F12之后發(fā)現(xiàn)爬取規(guī)則變了
一頓無語之后,重新定義了第二頁之后的規(guī)則,然后問題解決了

2017年4月17日 12:50
編輯回答
久礙你

給你點思路,下面是我之前寫的
圖片描述

2018年4月9日 12:47