python - 使用scrapy抓取页面

标签 python scrapy

我是 scrapy 的新手。 我想在 this 上抓取产品页。我的代码只抓取了第一页,也抓取了大约 15 种产品,然后它停止了。并且也想抓取下一页。有什么帮助吗?

这是我的课

class AllyouneedSpider(CrawlSpider):
   name = "allyouneed"
allowed_domains = ["de.allyouneed.com"]


start_urls = [ 'http://de.allyouneed.com/de/sportschuhe-/8799665488014/',]

rules = (
    Rule(LxmlLinkExtractor(allow=(), restrict_xpaths='//*[@class="itm fst jf-lDiv"]//a[@href]'), callback='parse_obj', process_links="parse_filter") ,
    Rule(LxmlLinkExtractor(restrict_xpaths='//*[@id="M62_searchhit"]//a[@href]')),

    )

def parse_filter(self, links):
    for link in links:
        if self.allowed_domains[0] not in link.url:
            pass  # print link.url
        # print links
    return links



def parse_obj(self, response):
    item = AllyouneedItem()
    sel = scrapy.Selector(response)
    item['url'] = []
    url = response.selector.xpath('//*[@id="M62_searchhit"]//a[@href]').extract()
    ti = response.selector.xpath('//span[@itemprop="name"]/text()').extract()
    dec = response.selector.xpath('//div[@class="m-desc m-desc-t"]//text()').extract()
    cat = response.selector.xpath('//span[@itemprop="title"]/text()').extract()

    if ti:
        item['title'] = ti
        item['url'] = response.url
        item['category'] = cat
        item['decription'] = dec
        print item
        yield item

最佳答案

使用 restrict_xpaths=('//a[@class="nxtPge"]') 将找到下一页的链接,无需找到所有链接,只需找到那个链接.您也不需要过滤 URL,因为 scrapy 默认会这样做。

Rule(LinkExtractor(allow=(), restrict_xpaths='//a[@class="nxtPge"]', callback='parse_obj')

您还可以通过删除选择器部分而不是初始化项目来简化 parse_obj(),

item = AllyouneedItem()
url = response.xpath( etc...

关于python - 使用scrapy抓取页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271570/

相关文章:

python 列出复制是深复制还是浅复制?它是如何完成的?

python - 如何在 PYTHON 中的列表字典中迭代值

python - Scrapy csv输出每列没有多行

php - 编写一个程序来抓取论坛

python - pyspark Pandas udf 运行时错误 : Number of columns of the returned doesn't match specified schema

python - Gremlin Python - 连接到多节点 JanusGraph 集群

python - 将张量分配给多个切片

python - Scrapy-类型错误: 'Request' object is not iterable

python - 在Scrapy(网络爬虫)中返回复杂的项目

python - Scrapy只爬取一页